{"id":1033410,"date":"2024-12-31T11:41:37","date_gmt":"2024-12-31T03:41:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1033410.html"},"modified":"2024-12-31T11:41:40","modified_gmt":"2024-12-31T03:41:40","slug":"python%e4%b8%ad%e4%ba%8c%e8%bf%9b%e5%88%b6%e5%a6%82%e4%bd%95%e5%8f%96%e5%8f%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1033410.html","title":{"rendered":"python\u4e2d\u4e8c\u8fdb\u5236\u5982\u4f55\u53d6\u53cd"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/951edc8f-6f4a-4c18-a1a2-d1e52c7319c8.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u4e8c\u8fdb\u5236\u5982\u4f55\u53d6\u53cd\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u53d6\u53cd\u64cd\u4f5c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\u201c<del>\u201d\u6765\u5b8c\u6210\u3002<strong>\u4f4d\u8fd0\u7b97\u7b26\u201c<\/del>\u201d\u662f\u6309\u4f4d\u53d6\u53cd\u64cd\u4f5c\u7b26\u3001\u5c06\u6bcf\u4e00\u4f4d\u4e0a\u76840\u53d8\u4e3a1\uff0c1\u53d8\u4e3a0<\/strong>\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6570\u20181010\u2019\uff0c\u4f7f\u7528\u6309\u4f4d\u53d6\u53cd\u64cd\u4f5c\u7b26\u4e4b\u540e\u4f1a\u53d8\u6210\u20180101\u2019\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u89e3\u91ca\u5e76\u5c55\u793a\u5982\u4f55\u5728Python\u4e2d\u6267\u884c\u4e8c\u8fdb\u5236\u53d6\u53cd\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6309\u4f4d\u53d6\u53cd\u8fd0\u7b97\u7b26\u7684\u57fa\u672c\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u6309\u4f4d\u53d6\u53cd\u8fd0\u7b97\u7b26\u201c~\u201d\u7528\u4e8e\u5bf9\u6574\u6570\u8fdb\u884c\u6309\u4f4d\u53d6\u53cd\u64cd\u4f5c\u3002\u5728Python\u4e2d\uff0c\u6240\u6709\u6574\u6570\u90fd\u662f\u4ee5\u8865\u7801\u5f62\u5f0f\u5b58\u50a8\u7684\uff0c\u8fd9\u610f\u5473\u7740\u6b63\u6570\u548c\u8d1f\u6570\u5728\u5185\u5b58\u4e2d\u7684\u5b58\u50a8\u5f62\u5f0f\u4e0d\u540c\u3002\u5728\u89e3\u91ca\u53d6\u53cd\u64cd\u4f5c\u524d\uff0c\u5148\u4e86\u89e3\u4e00\u4e0b\u8865\u7801\u7684\u8868\u793a\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8865\u7801\u8868\u793a\u65b9\u5f0f<\/h4>\n<\/p>\n<p><p>\u5728\u4e8c\u8fdb\u5236\u4e2d\uff0c\u6b63\u6570\u548c\u8d1f\u6570\u7684\u8868\u793a\u65b9\u6cd5\u6709\u6240\u4e0d\u540c\u3002\u6b63\u6570\u7684\u8865\u7801\u8868\u793a\u4e0e\u5176\u539f\u7801\u76f8\u540c\uff0c\u800c\u8d1f\u6570\u7684\u8865\u7801\u8868\u793a\u5219\u662f\u5b83\u7684\u53cd\u7801\u52a01\u3002\u4f8b\u5982\uff0c\u6574\u65704\u7684\u8865\u7801\u8868\u793a\u4e3a<code>0000 0100<\/code>\uff0c\u800c-4\u7684\u8865\u7801\u8868\u793a\u4e3a<code>1111 1100<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6309\u4f4d\u53d6\u53cd\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4efb\u610f\u6574\u6570n\uff0c\u6309\u4f4d\u53d6\u53cd\u64cd\u4f5c\u7b26\u201c~\u201d\u5c06\u8fd4\u56de<code>-(n + 1)<\/code>\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">n = 4<\/p>\n<p>result = ~n<\/p>\n<p>print(result)  # \u8f93\u51fa: -5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e8c\u8fdb\u5236\u5c42\u9762\uff0c\u8fd9\u610f\u5473\u7740\u6bcf\u4e00\u4f4d\u4e0a\u76840\u53d8\u4e3a1\uff0c1\u53d8\u4e3a0\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><h4>1\u3001\u6b63\u6570\u53d6\u53cd<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">n = 10<\/p>\n<p>result = ~n<\/p>\n<p>print(f&quot;\u539f\u6570: {n}, \u53d6\u53cd\u540e\u7684\u6570: {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u89e3\u91ca\uff1a\u5bf9\u4e8e\u6574\u657010\uff0c\u5176\u4e8c\u8fdb\u5236\u8868\u793a\u4e3a<code>0000 1010<\/code>\uff0c\u6309\u4f4d\u53d6\u53cd\u540e\u53d8\u4e3a<code>1111 0101<\/code>\uff0c\u5373-11\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8d1f\u6570\u53d6\u53cd<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">n = -10<\/p>\n<p>result = ~n<\/p>\n<p>print(f&quot;\u539f\u6570: {n}, \u53d6\u53cd\u540e\u7684\u6570: {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u89e3\u91ca\uff1a\u5bf9\u4e8e\u6574\u6570-10\uff0c\u5176\u8865\u7801\u8868\u793a\u4e3a<code>1111 0110<\/code>\uff0c\u6309\u4f4d\u53d6\u53cd\u540e\u53d8\u4e3a<code>0000 1001<\/code>\uff0c\u53739\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5b9e\u9645\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001\u63a9\u7801\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u63a9\u7801\u64cd\u4f5c\u662f\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u901a\u8fc7\u63a9\u7801\u53ef\u4ee5\u5bf9\u67d0\u4e9b\u4f4d\u8fdb\u884c\u7279\u5b9a\u7684\u64cd\u4f5c\u3002\u6309\u4f4d\u53d6\u53cd\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u63a9\u7801\u3002\u4f8b\u5982\uff0c\u6e05\u9664\u67d0\u4e9b\u7279\u5b9a\u4f4d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def clear_bits(n, mask):<\/p>\n<p>    return n &amp; ~mask<\/p>\n<p>n = 0b10101010<\/p>\n<p>mask = 0b00001111<\/p>\n<p>result = clear_bits(n, mask)<\/p>\n<p>print(f&quot;\u539f\u6570: {bin(n)}, \u63a9\u7801: {bin(mask)}, \u7ed3\u679c: {bin(result)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u89e3\u91ca\uff1a\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u63a9\u7801<code>00001111<\/code>\uff0c\u5e76\u5bf9\u539f\u6570<code>10101010<\/code>\u8fdb\u884c\u6309\u4f4d\u53d6\u53cd\u548c\u6309\u4f4d\u4e0e\u64cd\u4f5c\uff0c\u7ed3\u679c\u4e3a<code>10100000<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7ffb\u8f6c\u4f4d<\/h4>\n<\/p>\n<p><p>\u6309\u4f4d\u53d6\u53cd\u53ef\u4ee5\u7528\u4e8e\u7ffb\u8f6c\u7279\u5b9a\u4f4d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def flip_bits(n, mask):<\/p>\n<p>    return n ^ mask<\/p>\n<p>n = 0b10101010<\/p>\n<p>mask = 0b00001111<\/p>\n<p>result = flip_bits(n, mask)<\/p>\n<p>print(f&quot;\u539f\u6570: {bin(n)}, \u63a9\u7801: {bin(mask)}, \u7ed3\u679c: {bin(result)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u89e3\u91ca\uff1a\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u63a9\u7801<code>00001111<\/code>\uff0c\u5e76\u5bf9\u539f\u6570<code>10101010<\/code>\u8fdb\u884c\u6309\u4f4d\u5f02\u6216\u64cd\u4f5c\uff0c\u7ed3\u679c\u4e3a<code>10100101<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><h4>1\u3001\u6ea2\u51fa\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6309\u4f4d\u53d6\u53cd\u64cd\u4f5c\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u6574\u6570\u6ea2\u51fa\u95ee\u9898\u3002\u5728Python\u4e2d\uff0c\u6574\u6570\u662f\u4efb\u610f\u7cbe\u5ea6\u7684\uff0c\u4e0d\u4f1a\u53d1\u751f\u6ea2\u51fa\uff0c\u4f46\u5728\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff08\u5982C\u8bed\u8a00\uff09\uff0c\u9700\u8981\u7279\u522b\u6ce8\u610f\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7b26\u53f7\u4f4d\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5e26\u7b26\u53f7\u7684\u6574\u6570\uff0c\u6309\u4f4d\u53d6\u53cd\u4f1a\u5f71\u54cd\u7b26\u53f7\u4f4d\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e8\u4f4d\u5e26\u7b26\u53f7\u6574\u6570\uff0c\u6309\u4f4d\u53d6\u53cd\u64cd\u4f5c\u4f1a\u5c06\u6b63\u6570\u53d8\u4e3a\u8d1f\u6570\uff0c\u8d1f\u6570\u53d8\u4e3a\u6b63\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u9762\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86Python\u4e2d\u5982\u4f55\u4f7f\u7528\u6309\u4f4d\u53d6\u53cd\u8fd0\u7b97\u7b26\u201c<del>\u201d\u6765\u8fdb\u884c\u4e8c\u8fdb\u5236\u53d6\u53cd\u64cd\u4f5c\u3002<strong>\u6309\u4f4d\u53d6\u53cd\u8fd0\u7b97\u7b26\u201c<\/del>\u201d\u7528\u4e8e\u5bf9\u6574\u6570\u8fdb\u884c\u6309\u4f4d\u53d6\u53cd\u64cd\u4f5c<\/strong>\uff0c\u5e76\u4e14\u5728\u4e8c\u8fdb\u5236\u8fd0\u7b97\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u638c\u63e1\u8fd9\u4e9b\u57fa\u672c\u64cd\u4f5c\uff0c\u5bf9\u4e8e\u6df1\u5165\u7406\u89e3\u8ba1\u7b97\u673a\u79d1\u5b66\u548c\u7f16\u7a0b\u6280\u5de7\u662f\u975e\u5e38\u6709\u5e2e\u52a9\u7684\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u4e8c\u8fdb\u5236\u53d6\u53cd\u64cd\u4f5c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6309\u4f4d\u53d6\u53cd\u8fd0\u7b97\u7b26<code>~<\/code>\u6765\u5b9e\u73b0\u4e8c\u8fdb\u5236\u53d6\u53cd\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u6574\u6570<code>x<\/code>\uff0c\u5176\u4e8c\u8fdb\u5236\u53d6\u53cd\u53ef\u4ee5\u901a\u8fc7<code>~x<\/code>\u6765\u83b7\u5f97\u3002\u8be5\u8fd0\u7b97\u7b26\u4f1a\u5bf9\u6570\u5b57\u7684\u6bcf\u4e00\u4f4d\u8fdb\u884c\u53cd\u8f6c\uff0c\u53730\u53d8\u4e3a1\uff0c1\u53d8\u4e3a0\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u53d6\u53cd\u540e\u7ed3\u679c\u4f1a\u662f\u4e00\u4e2a\u8d1f\u6570\uff0c\u56e0\u4e3aPython\u4f7f\u7528\u8865\u7801\u8868\u793a\u8d1f\u6570\u3002<\/p>\n<p><strong>\u4e8c\u8fdb\u5236\u53d6\u53cd\u548c\u8865\u7801\u7684\u5173\u7cfb\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u8ba1\u7b97\u673a\u4e2d\uff0c\u8d1f\u6570\u901a\u5e38\u7528\u8865\u7801\u8868\u793a\u3002\u53d6\u53cd\u64cd\u4f5c\u4f1a\u6539\u53d8\u6570\u5b57\u7684\u7b26\u53f7\u3002\u5bf9\u4e8e\u4e00\u4e2a\u6b63\u6574\u6570\uff0c\u5176\u53d6\u53cd\u540e\u4f1a\u4ea7\u751f\u5bf9\u5e94\u7684\u8d1f\u6570\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u6574\u6570<code>5<\/code>\uff08\u4e8c\u8fdb\u5236\u4e3a<code>0000 0101<\/code>\uff09\uff0c\u53d6\u53cd\u540e\u53d8\u4e3a<code>1111 1010<\/code>\uff0c\u5373-6\u3002\u7406\u89e3\u8865\u7801\u7684\u539f\u7406\u5bf9\u4e8e\u5728\u4e8c\u8fdb\u5236\u4e2d\u8fdb\u884c\u53d6\u53cd\u64cd\u4f5c\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u53d6\u53cd\u540e\u7684\u7ed3\u679c\u8f6c\u6362\u56de\u539f\u59cb\u7684\u4e8c\u8fdb\u5236\u683c\u5f0f\uff1f<\/strong><br \/>\u53d6\u53cd\u540e\u7684\u7ed3\u679c\u53ef\u4ee5\u901a\u8fc7<code>bin()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u3002\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>bin(~x)<\/code>\u53ef\u4ee5\u5f97\u5230\u53d6\u53cd\u7ed3\u679c\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>bin()<\/code>\u51fd\u6570\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u4f1a\u5305\u542b\u524d\u7f00<code>0b<\/code>\uff0c\u8868\u793a\u8fd9\u662f\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6570\u3002\u5982\u679c\u60f3\u8981\u53bb\u6389\u8fd9\u4e2a\u524d\u7f00\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u64cd\u4f5c\uff0c\u4f8b\u5982<code>bin(~x)[2:]<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53d6\u53cd\u64cd\u4f5c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\u201c\u201d\u6765\u5b8c\u6210\u3002\u4f4d\u8fd0\u7b97\u7b26\u201c\u201d\u662f\u6309\u4f4d\u53d6\u53cd\u64cd\u4f5c\u7b26\u3001\u5c06\u6bcf\u4e00\u4f4d\u4e0a\u76840\u53d8\u4e3a1\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1033424,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1033410"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1033410"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1033410\/revisions"}],"predecessor-version":[{"id":1033428,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1033410\/revisions\/1033428"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1033424"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1033410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1033410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1033410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}