{"id":1100674,"date":"2025-01-08T15:42:15","date_gmt":"2025-01-08T07:42:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1100674.html"},"modified":"2025-01-08T15:42:18","modified_gmt":"2025-01-08T07:42:18","slug":"python%e5%a6%82%e4%bd%95%e4%ba%8c%e8%bf%9b%e5%88%b6%e5%8f%96%e5%8f%8d-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1100674.html","title":{"rendered":"Python\u5982\u4f55\u4e8c\u8fdb\u5236\u53d6\u53cd"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063820\/8974c4b4-187e-4a3d-93dd-59ef32fbf0ad.webp\" alt=\"Python\u5982\u4f55\u4e8c\u8fdb\u5236\u53d6\u53cd\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u8fdb\u884c\u4e8c\u8fdb\u5236\u53d6\u53cd\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>~<\/code>\u8fd0\u7b97\u7b26\u3001\u4f7f\u7528\u4f4d\u64cd\u4f5c\u51fd\u6570\u4ee5\u53ca\u81ea\u5df1\u7f16\u5199\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/strong> \u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\u6765\u5e2e\u52a9\u4f60\u7406\u89e3\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u4e8c\u8fdb\u5236\u53d6\u53cd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001<code>~<\/code> \u8fd0\u7b97\u7b26<\/h3>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u7684\u6309\u4f4d\u53d6\u53cd\u8fd0\u7b97\u7b26 <code>~<\/code>\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u5bf9\u6574\u6570\u8fdb\u884c\u4e8c\u8fdb\u5236\u53d6\u53cd\u64cd\u4f5c\u3002\u5177\u4f53\u6765\u8bf4\uff0c<code>~x<\/code> \u7684\u7ed3\u679c\u662f <code>-(x+1)<\/code>\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>result = ~x<\/p>\n<p>print(result)  # \u8f93\u51fa -11<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c10 \u7684\u4e8c\u8fdb\u5236\u8868\u793a\u662f <code>1010<\/code>\uff0c\u53d6\u53cd\u540e\u5f97\u5230 <code>0101<\/code>\uff08\u5373 -11\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u4f4d\u64cd\u4f5c\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4f4d\u64cd\u4f5c\u51fd\u6570\u6765\u5b9e\u73b0\u4e8c\u8fdb\u5236\u53d6\u53cd\u3002\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528 <code>bin()<\/code> \u51fd\u6570\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u8fdb\u884c\u53d6\u53cd\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def binary_not(x, bits=32):<\/p>\n<p>    binary_str = bin(x)[2:]  # \u53bb\u6389 &#39;0b&#39; \u524d\u7f00<\/p>\n<p>    binary_str = binary_str.zfill(bits)  # \u8865\u8db3\u4f4d\u6570<\/p>\n<p>    inverted_str = &#39;&#39;.join(&#39;1&#39; if bit == &#39;0&#39; else &#39;0&#39; for bit in binary_str)<\/p>\n<p>    return int(inverted_str, 2)<\/p>\n<p>x = 10<\/p>\n<p>result = binary_not(x)<\/p>\n<p>print(result)  # \u8f93\u51fa 4294967285<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u6574\u6570 <code>10<\/code> \u8f6c\u6362\u4e3a32\u4f4d\u7684\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\uff0c\u5e76\u53d6\u53cd\u5f97\u5230 <code>4294967285<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u81ea\u5df1\u7f16\u5199\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u5185\u7f6e\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u81ea\u5df1\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u5b9e\u73b0\u4e8c\u8fdb\u5236\u53d6\u53cd\u3002\u8fd9\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u4e8c\u8fdb\u5236\u8fd0\u7b97\u7684\u539f\u7406\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def custom_binary_not(x, bits=32):<\/p>\n<p>    result = 0<\/p>\n<p>    for i in range(bits):<\/p>\n<p>        if not (x &amp; (1 &lt;&lt; i)):<\/p>\n<p>            result |= (1 &lt;&lt; i)<\/p>\n<p>    return result<\/p>\n<p>x = 10<\/p>\n<p>result = custom_binary_not(x)<\/p>\n<p>print(result)  # \u8f93\u51fa 4294967285<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9010\u4f4d\u68c0\u67e5\u8f93\u5165\u6574\u6570\u7684\u6bcf\u4e00\u4f4d\u662f\u5426\u4e3a <code>0<\/code>\uff0c\u5982\u679c\u662f\uff0c\u5219\u5c06\u5bf9\u5e94\u4f4d\u8bbe\u7f6e\u4e3a <code>1<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>1\u3001\u63a9\u7801\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5728\u8ba1\u7b97\u673a\u7f16\u7a0b\u4e2d\uff0c\u63a9\u7801\u64cd\u4f5c\u662f\u975e\u5e38\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u3002\u901a\u8fc7\u4e8c\u8fdb\u5236\u53d6\u53cd\uff0c\u6211\u4eec\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u63a9\u7801\u6765\u5c4f\u853d\u67d0\u4e9b\u4f4d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def create_mask(bits, mask_bits):<\/p>\n<p>    mask = (1 &lt;&lt; mask_bits) - 1<\/p>\n<p>    return ~mask &amp; ((1 &lt;&lt; bits) - 1)<\/p>\n<p>mask = create_mask(32, 8)<\/p>\n<p>print(bin(mask))  # \u8f93\u51fa 0b11111111111111111111111100000000<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c\u6211\u4eec\u751f\u6210\u4e86\u4e00\u4e2a\u63a9\u7801\uff0c\u7528\u4e8e\u5c4f\u853d32\u4f4d\u6574\u6570\u7684\u4f4e8\u4f4d\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6570\u636e\u52a0\u5bc6<\/h4>\n<\/p>\n<p><p>\u4e8c\u8fdb\u5236\u53d6\u53cd\u4e5f\u53ef\u4ee5\u7528\u4e8e\u7b80\u5355\u7684\u6570\u636e\u52a0\u5bc6\u3002\u901a\u8fc7\u5bf9\u6570\u636e\u8fdb\u884c\u53d6\u53cd\u64cd\u4f5c\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u96be\u4ee5\u8bc6\u522b\u7684\u5f62\u5f0f\uff0c\u4ece\u800c\u63d0\u9ad8\u6570\u636e\u7684\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def encrypt(data):<\/p>\n<p>    return ~data<\/p>\n<p>def decrypt(data):<\/p>\n<p>    return ~data<\/p>\n<p>original_data = 12345<\/p>\n<p>encrypted_data = encrypt(original_data)<\/p>\n<p>decrypted_data = decrypt(encrypted_data)<\/p>\n<p>print(original_data == decrypted_data)  # \u8f93\u51fa True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e8c\u8fdb\u5236\u53d6\u53cd\u6765\u52a0\u5bc6\u548c\u89e3\u5bc6\u6570\u636e\uff0c\u5e76\u9a8c\u8bc1\u52a0\u5bc6\u89e3\u5bc6\u540e\u7684\u6570\u636e\u4e0e\u539f\u59cb\u6570\u636e\u662f\u5426\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f4d\u6570\u9650\u5236<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u4e8c\u8fdb\u5236\u53d6\u53cd\u64cd\u4f5c\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4f4d\u6570\u9650\u5236\u3002Python \u4e2d\u7684\u6574\u6570\u662f\u65e0\u9650\u7cbe\u5ea6\u7684\uff0c\u4f46\u5728\u8fdb\u884c\u4f4d\u64cd\u4f5c\u65f6\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528\u56fa\u5b9a\u4f4d\u6570\uff08\u4f8b\u5982 32 \u4f4d\u6216 64 \u4f4d\uff09\u3002\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7b26\u53f7\u4f4d\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6709\u7b26\u53f7\u6574\u6570\uff0c\u6700\u9ad8\u4f4d\u901a\u5e38\u662f\u7b26\u53f7\u4f4d\u3002\u5728\u8fdb\u884c\u4e8c\u8fdb\u5236\u53d6\u53cd\u64cd\u4f5c\u65f6\uff0c\u9700\u8981\u7279\u522b\u6ce8\u610f\u7b26\u53f7\u4f4d\u7684\u5904\u7406\u3002\u5982\u679c\u4e0d\u5c0f\u5fc3\u5904\u7406\u7b26\u53f7\u4f4d\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7ed3\u679c\u4e0e\u9884\u671f\u4e0d\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u8ba8\u8bba\u4e86Python\u4e2d\u8fdb\u884c\u4e8c\u8fdb\u5236\u53d6\u53cd\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528 <code>~<\/code> \u8fd0\u7b97\u7b26\u3001\u4f4d\u64cd\u4f5c\u51fd\u6570\u4ee5\u53ca\u81ea\u5df1\u7f16\u5199\u51fd\u6570\u3002\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86\u4e00\u4e9b\u5e94\u7528\u573a\u666f\u548c\u6ce8\u610f\u4e8b\u9879\uff0c\u4ee5\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u4e8c\u8fdb\u5236\u53d6\u53cd\u64cd\u4f5c\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u8fdb\u884c\u4e8c\u8fdb\u5236\u53d6\u53cd\u64cd\u4f5c\uff0c\u4ece\u800c\u89e3\u51b3\u5404\u79cd\u5b9e\u9645\u95ee\u9898\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\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\u3002\u8fd9\u4e2a\u8fd0\u7b97\u7b26\u4f1a\u5c06\u6bcf\u4e00\u4f4d\u76840\u53d8\u4e3a1\uff0c1\u53d8\u4e3a0\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u6574\u6570<code>5<\/code>\uff08\u5176\u4e8c\u8fdb\u5236\u8868\u793a\u4e3a<code>0000 0101<\/code>\uff09\uff0c\u6267\u884c<code>~5<\/code>\u5c06\u5f97\u5230<code>-6<\/code>\uff08\u4e8c\u8fdb\u5236\u8868\u793a\u4e3a<code>1111 1010<\/code>\uff0c\u5728Python\u4e2d\u91c7\u7528\u8865\u7801\u8868\u793a\u6cd5\uff09\u3002<\/p>\n<p><strong>Python\u7684\u4e8c\u8fdb\u5236\u53d6\u53cd\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u6709\u4f55\u4e0d\u540c\uff1f<\/strong><br \/>Python\u7684\u4e8c\u8fdb\u5236\u53d6\u53cd\u4f7f\u7528\u7684\u662f\u8865\u7801\u8868\u793a\u6cd5\uff0c\u8fd9\u610f\u5473\u7740\u53d6\u53cd\u540e\u7ed3\u679c\u4f1a\u4ea7\u751f\u8d1f\u6570\u3002\u5728\u5176\u4ed6\u8bed\u8a00\u4e2d\uff0c\u5982C\u6216Java\uff0c\u53d6\u53cd\u64cd\u4f5c\u7684\u7ed3\u679c\u53ef\u80fd\u4f1a\u6839\u636e\u6574\u6570\u7c7b\u578b\u7684\u5927\u5c0f\u548c\u7b26\u53f7\u4f4d\u7684\u5b9a\u4e49\u6709\u6240\u4e0d\u540c\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528Python\u8fdb\u884c\u4e8c\u8fdb\u5236\u53d6\u53cd\u65f6\uff0c\u7406\u89e3\u8865\u7801\u7684\u6982\u5ff5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u67e5\u770b\u6574\u6570\u7684\u4e8c\u8fdb\u5236\u8868\u793a\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>bin()<\/code>\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c<code>bin(5)<\/code>\u5c06\u8fd4\u56de<code>&#39;0b101&#39;<\/code>\uff0c\u5176\u4e2d<code>0b<\/code>\u662f\u4e8c\u8fdb\u5236\u6570\u7684\u524d\u7f00\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u7528\u6237\u53ef\u4ee5\u76f4\u89c2\u5730\u67e5\u770b\u6574\u6570\u7684\u4e8c\u8fdb\u5236\u5f62\u5f0f\uff0c\u4ece\u800c\u66f4\u597d\u5730\u7406\u89e3\u53d6\u53cd\u64cd\u4f5c\u7684\u6548\u679c\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5bf9\u8d1f\u6570\u8fdb\u884c\u4e8c\u8fdb\u5236\u53d6\u53cd\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u5141\u8bb8\u5bf9\u8d1f\u6570\u8fdb\u884c\u4e8c\u8fdb\u5236\u53d6\u53cd\u3002\u53d6\u53cd\u64cd\u4f5c\u5c06\u5bf9\u8d1f\u6570\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u8fdb\u884c\u5904\u7406\uff0c\u7ed3\u679c\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6574\u6570\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e-5\uff0c\u6267\u884c<code>~-5<\/code>\u5c06\u8fd4\u56de4\u3002\u8fd9\u662f\u56e0\u4e3a\u53d6\u53cd\u64cd\u4f5c\u4f1a\u6539\u53d8\u7b26\u53f7\u4f4d\uff0c\u8fdb\u800c\u5f71\u54cd\u6574\u4f53\u7684\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u8fdb\u884c\u4e8c\u8fdb\u5236\u53d6\u53cd\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528~\u8fd0\u7b97\u7b26\u3001\u4f7f\u7528\u4f4d\u64cd\u4f5c\u51fd\u6570\u4ee5\u53ca\u81ea\u5df1\u7f16\u5199\u51fd\u6570\u6765\u5b9e\u73b0\u3002 \u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec [&hellip;]","protected":false},"author":3,"featured_media":1100684,"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\/1100674"}],"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=1100674"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100674\/revisions"}],"predecessor-version":[{"id":1100685,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100674\/revisions\/1100685"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1100684"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1100674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1100674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1100674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}