{"id":1036491,"date":"2024-12-31T12:07:28","date_gmt":"2024-12-31T04:07:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1036491.html"},"modified":"2024-12-31T12:07:30","modified_gmt":"2024-12-31T04:07:30","slug":"python%e5%a6%82%e4%bd%95%e6%93%8d%e4%bd%9c%e4%ba%8c%e8%bf%9b%e5%88%b6%e8%ae%a1%e7%ae%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1036491.html","title":{"rendered":"python\u5982\u4f55\u64cd\u4f5c\u4e8c\u8fdb\u5236\u8ba1\u7b97"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/77be290b-1ac6-4f17-94be-eef1086c172b.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u64cd\u4f5c\u4e8c\u8fdb\u5236\u8ba1\u7b97\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d: <\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u4f4d\u8fd0\u7b97\u7b26\u3001\u683c\u5f0f\u5316\u8f93\u51fa\u3001\u5e93\u51fd\u6570<\/strong>\u7b49\u662fPython\u4e2d\u8fdb\u884c\u4e8c\u8fdb\u5236\u8ba1\u7b97\u7684\u4e3b\u8981\u65b9\u5f0f\u3002<strong>\u4f4d\u8fd0\u7b97\u7b26<\/strong>\u662f\u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u5b83\u4eec\u5305\u62ec\u6309\u4f4d\u4e0e(&amp;)\u3001\u6309\u4f4d\u6216(|)\u3001\u6309\u4f4d\u5f02\u6216(^)\u3001\u6309\u4f4d\u53d6\u53cd(~)\u3001\u5de6\u79fb(&lt;&lt;)\u3001\u53f3\u79fb(&gt;&gt;)\uff0c\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u53ef\u4ee5\u76f4\u63a5\u5bf9\u6574\u6570\u8fdb\u884c\u4e8c\u8fdb\u5236\u64cd\u4f5c\u3002<strong>\u5185\u7f6e\u51fd\u6570<\/strong>\u5982bin()\u3001int()\u7b49\u4e5f\u975e\u5e38\u6709\u7528\uff0c\u5b83\u4eec\u53ef\u4ee5\u5728\u4e8c\u8fdb\u5236\u4e0e\u5341\u8fdb\u5236\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u8fdb\u884c\u4e8c\u8fdb\u5236\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8fdb\u884c\u4e8c\u8fdb\u5236\u8ba1\u7b97<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u4e8c\u8fdb\u5236\u8ba1\u7b97\u3002\u4f8b\u5982\uff0cbin()\u51fd\u6570\u53ef\u4ee5\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\uff0c\u800cint()\u51fd\u6570\u53ef\u4ee5\u5c06\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32<\/p>\n<p>binary_str = bin(10)  # \u8f93\u51fa: &#39;0b1010&#39;<\/p>\n<h2><strong>\u5c06\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570<\/strong><\/h2>\n<p>decimal_int = int(&#39;1010&#39;, 2)  # \u8f93\u51fa: 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u5185\u7f6e\u51fd\u6570\u4f7f\u5f97\u5728\u4e8c\u8fdb\u5236\u548c\u5341\u8fdb\u5236\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3002\u901a\u8fc7\u8fd9\u4e9b\u8f6c\u6362\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u4e0d\u540c\u8fdb\u5236\u4e4b\u95f4\u8fdb\u884c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\u8fdb\u884c\u4e8c\u8fdb\u5236\u8ba1\u7b97<\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684\u4f4d\u8fd0\u7b97\u7b26\u5141\u8bb8\u6211\u4eec\u76f4\u63a5\u5bf9\u6574\u6570\u8fdb\u884c\u4e8c\u8fdb\u5236\u64cd\u4f5c\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u4f4d\u8fd0\u7b97\u7b26\u53ca\u5176\u7528\u9014\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6309\u4f4d\u4e0e(&amp;)\uff1a\u5bf9\u4e24\u4e2a\u6574\u6570\u7684\u6bcf\u4e00\u4f4d\u8fdb\u884c\u4e0e\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">a = 10  # \u4e8c\u8fdb\u5236: 1010<\/p>\n<p>b = 12  # \u4e8c\u8fdb\u5236: 1100<\/p>\n<p>result = a &amp; b  # \u8f93\u51fa: 8 (\u4e8c\u8fdb\u5236: 1000)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u6309\u4f4d\u6216(|)\uff1a\u5bf9\u4e24\u4e2a\u6574\u6570\u7684\u6bcf\u4e00\u4f4d\u8fdb\u884c\u6216\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">a = 10  # \u4e8c\u8fdb\u5236: 1010<\/p>\n<p>b = 12  # \u4e8c\u8fdb\u5236: 1100<\/p>\n<p>result = a | b  # \u8f93\u51fa: 14 (\u4e8c\u8fdb\u5236: 1110)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u6309\u4f4d\u5f02\u6216(^)\uff1a\u5bf9\u4e24\u4e2a\u6574\u6570\u7684\u6bcf\u4e00\u4f4d\u8fdb\u884c\u5f02\u6216\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">a = 10  # \u4e8c\u8fdb\u5236: 1010<\/p>\n<p>b = 12  # \u4e8c\u8fdb\u5236: 1100<\/p>\n<p>result = a ^ b  # \u8f93\u51fa: 6 (\u4e8c\u8fdb\u5236: 0110)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li>\u6309\u4f4d\u53d6\u53cd(~)\uff1a\u5bf9\u6574\u6570\u7684\u6bcf\u4e00\u4f4d\u8fdb\u884c\u53d6\u53cd\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">a = 10  # \u4e8c\u8fdb\u5236: 1010<\/p>\n<p>result = ~a  # \u8f93\u51fa: -11 (\u4e8c\u8fdb\u5236: 11111111111111111111111111110101)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"5\">\n<li>\u5de6\u79fb(&lt;&lt;)\uff1a\u5c06\u6574\u6570\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u5411\u5de6\u79fb\u52a8\u6307\u5b9a\u7684\u4f4d\u6570\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">a = 10  # \u4e8c\u8fdb\u5236: 1010<\/p>\n<p>result = a &lt;&lt; 2  # \u8f93\u51fa: 40 (\u4e8c\u8fdb\u5236: 101000)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"6\">\n<li>\u53f3\u79fb(&gt;&gt;)\uff1a\u5c06\u6574\u6570\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u5411\u53f3\u79fb\u52a8\u6307\u5b9a\u7684\u4f4d\u6570\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">a = 10  # \u4e8c\u8fdb\u5236: 1010<\/p>\n<p>result = a &gt;&gt; 2  # \u8f93\u51fa: 2 (\u4e8c\u8fdb\u5236: 10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u683c\u5f0f\u5316\u8f93\u51fa\u4e8c\u8fdb\u5236\u6570<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u4e8c\u8fdb\u5236\u8ba1\u7b97\u65f6\uff0c\u5e38\u5e38\u9700\u8981\u5c06\u7ed3\u679c\u4ee5\u4e8c\u8fdb\u5236\u5f62\u5f0f\u8f93\u51fa\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u529f\u80fd\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>binary_str = f&#39;{a:b}&#39;  # \u8f93\u51fa: &#39;1010&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\uff0c\u5e76\u4ee5\u6307\u5b9a\u7684\u683c\u5f0f\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u5e93\u51fd\u6570\u8fdb\u884c\u4e8c\u8fdb\u5236\u8ba1\u7b97<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570\u548c\u4f4d\u8fd0\u7b97\u7b26\u53ef\u80fd\u4e0d\u591f\u65b9\u4fbf\u3002\u8fd9\u65f6\uff0c\u53ef\u4ee5\u501f\u52a9\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982bitarray\u548cnumpy\uff0c\u6765\u8fdb\u884c\u4e8c\u8fdb\u5236\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528bitarray\u5e93<\/li>\n<\/ol>\n<p><p>bitarray\u662f\u4e00\u4e2a\u9ad8\u6548\u7684\u4f4d\u6570\u7ec4\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u4e8c\u8fdb\u5236\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bitarray import bitarray<\/p>\n<p>a = bitarray(&#39;1010&#39;)<\/p>\n<p>b = bitarray(&#39;1100&#39;)<\/p>\n<h2><strong>\u6309\u4f4d\u4e0e<\/strong><\/h2>\n<p>result = a &amp; b  # \u8f93\u51fa: bitarray(&#39;1000&#39;)<\/p>\n<h2><strong>\u6309\u4f4d\u6216<\/strong><\/h2>\n<p>result = a | b  # \u8f93\u51fa: bitarray(&#39;1110&#39;)<\/p>\n<h2><strong>\u6309\u4f4d\u5f02\u6216<\/strong><\/h2>\n<p>result = a ^ b  # \u8f93\u51fa: bitarray(&#39;0110&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528numpy\u5e93<\/li>\n<\/ol>\n<p><p>numpy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u652f\u6301\u591a\u79cd\u7c7b\u578b\u7684\u6570\u636e\u64cd\u4f5c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528numpy\u8fdb\u884c\u4e8c\u8fdb\u5236\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>a = np.array([10], dtype=np.uint8)<\/p>\n<p>b = np.array([12], dtype=np.uint8)<\/p>\n<h2><strong>\u6309\u4f4d\u4e0e<\/strong><\/h2>\n<p>result = np.bitwise_and(a, b)  # \u8f93\u51fa: array([8], dtype=uint8)<\/p>\n<h2><strong>\u6309\u4f4d\u6216<\/strong><\/h2>\n<p>result = np.bitwise_or(a, b)  # \u8f93\u51fa: array([14], dtype=uint8)<\/p>\n<h2><strong>\u6309\u4f4d\u5f02\u6216<\/strong><\/h2>\n<p>result = np.bitwise_xor(a, b)  # \u8f93\u51fa: array([6], dtype=uint8)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u4e8c\u8fdb\u5236\u8ba1\u7b97\u7684\u5b9e\u9645\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u4e8c\u8fdb\u5236\u8ba1\u7b97\u5728\u8bb8\u591a\u5b9e\u9645\u5e94\u7528\u4e2d\u90fd\u975e\u5e38\u91cd\u8981\uff0c\u7279\u522b\u662f\u5728\u4f4e\u7ea7\u522b\u7684\u7cfb\u7edf\u7f16\u7a0b\u3001\u7f51\u7edc\u7f16\u7a0b\u548c\u6570\u636e\u538b\u7f29\u4e2d\u3002<\/p>\n<\/p>\n<ol>\n<li>\u7f51\u7edc\u7f16\u7a0b\u4e2d\u7684\u4e8c\u8fdb\u5236\u8ba1\u7b97<\/li>\n<\/ol>\n<p><p>\u5728\u7f51\u7edc\u7f16\u7a0b\u4e2d\uff0cIP\u5730\u5740\u548c\u7aef\u53e3\u901a\u5e38\u4ee5\u4e8c\u8fdb\u5236\u5f62\u5f0f\u8868\u793a\u3002\u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\u53ef\u4ee5\u65b9\u4fbf\u5730\u64cd\u4f5c\u8fd9\u4e9b\u6570\u636e\u3002\u4f8b\u5982\uff0c\u83b7\u53d6IP\u5730\u5740\u7684\u7f51\u7edc\u90e8\u5206\u548c\u4e3b\u673a\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ip = 0b11000000101010000000000100000001  # 192.168.1.1<\/p>\n<p>mask = 0b11111111111111110000000000000000  # 255.255.0.0<\/p>\n<p>network = ip &amp; mask  # \u8f93\u51fa: 0b11000000101010000000000000000000 (192.168.0.0)<\/p>\n<p>host = ip &amp; ~mask  # \u8f93\u51fa: 0b00000000000000000000000100000001 (0.0.1.1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u6570\u636e\u538b\u7f29\u4e2d\u7684\u4e8c\u8fdb\u5236\u8ba1\u7b97<\/li>\n<\/ol>\n<p><p>\u5728\u6570\u636e\u538b\u7f29\u4e2d\uff0c\u4f4d\u64cd\u4f5c\u53ef\u4ee5\u7528\u4e8e\u7f16\u7801\u548c\u89e3\u7801\u6570\u636e\u3002\u4f8b\u5982\uff0cHuffman\u7f16\u7801\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u538b\u7f29\u7b97\u6cd5\uff0c\u5b83\u4f7f\u7528\u4e8c\u8fdb\u5236\u6811\u6765\u8868\u793a\u6570\u636e\u7684\u538b\u7f29\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u4f4d\u64cd\u4f5c\u8fdb\u884cHuffman\u7f16\u7801\u3002<\/p>\n<h2><strong>\u5b9e\u9645\u7684Huffman\u7f16\u7801\u7b97\u6cd5\u4f1a\u590d\u6742\u5f97\u591a\uff0c\u9700\u8981\u6784\u5efa\u4e8c\u8fdb\u5236\u6811\u3002<\/strong><\/h2>\n<p>data = &#39;hello&#39;<\/p>\n<p>frequency = {&#39;h&#39;: 1, &#39;e&#39;: 1, &#39;l&#39;: 2, &#39;o&#39;: 1}<\/p>\n<h2><strong>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u7f16\u7801\u8868<\/strong><\/h2>\n<p>encoding = {&#39;h&#39;: &#39;00&#39;, &#39;e&#39;: &#39;01&#39;, &#39;l&#39;: &#39;10&#39;, &#39;o&#39;: &#39;11&#39;}<\/p>\n<p>encoded_data = &#39;&#39;.join(encoding[char] for char in data)  # \u8f93\u51fa: &#39;0001101011&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u4e8c\u8fdb\u5236\u8ba1\u7b97\u7684\u6027\u80fd\u4f18\u5316<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u5927\u89c4\u6a21\u7684\u4e8c\u8fdb\u5236\u8ba1\u7b97\u65f6\uff0c\u6027\u80fd\u4f18\u5316\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6027\u80fd\u4f18\u5316\u7684\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\u4ee3\u66ff\u7b97\u672f\u8fd0\u7b97\u7b26<\/li>\n<\/ol>\n<p><p>\u4f4d\u8fd0\u7b97\u7b26\u901a\u5e38\u6bd4\u7b97\u672f\u8fd0\u7b97\u7b26\u66f4\u9ad8\u6548\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5de6\u79fb\u64cd\u4f5c\u4ee3\u66ff\u4e58\u6cd5\uff0c\u4f7f\u7528\u53f3\u79fb\u64cd\u4f5c\u4ee3\u66ff\u9664\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5de6\u79fb\u64cd\u4f5c\u4ee3\u66ff\u4e58\u6cd5<\/p>\n<p>result = 10 &lt;&lt; 1  # \u7b49\u4ef7\u4e8e: 10 * 2<\/p>\n<h2><strong>\u4f7f\u7528\u53f3\u79fb\u64cd\u4f5c\u4ee3\u66ff\u9664\u6cd5<\/strong><\/h2>\n<p>result = 10 &gt;&gt; 1  # \u7b49\u4ef7\u4e8e: 10 \/ 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528numpy\u6216\u5176\u4ed6\u9ad8\u6548\u5e93<\/li>\n<\/ol>\n<p><p>\u5bf9\u4e8e\u5927\u89c4\u6a21\u7684\u4e8c\u8fdb\u5236\u8ba1\u7b97\uff0c\u4f7f\u7528numpy\u6216\u5176\u4ed6\u9ad8\u6548\u5e93\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u5927\u89c4\u6a21\u6570\u636e<\/strong><\/h2>\n<p>data = np.random.randint(0, 256, size=1000000, dtype=np.uint8)<\/p>\n<h2><strong>\u4f7f\u7528numpy\u8fdb\u884c\u9ad8\u6548\u7684\u4e8c\u8fdb\u5236\u8ba1\u7b97<\/strong><\/h2>\n<p>result = np.bitwise_and(data, 0b11110000)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8f6c\u6362<\/li>\n<\/ol>\n<p><p>\u5728\u8fdb\u884c\u4e8c\u8fdb\u5236\u8ba1\u7b97\u65f6\uff0c\u5c3d\u91cf\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8fdb\u5236\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6570\u636e\u5df2\u7ecf\u662f\u4e8c\u8fdb\u5236\u683c\u5f0f\uff0c\u5c31\u4e0d\u8981\u518d\u5c06\u5176\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u8fdb\u884c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8f6c\u6362<\/p>\n<p>binary_data = &#39;1010&#39;<\/p>\n<p>result = int(binary_data, 2) &lt;&lt; 1  # \u76f4\u63a5\u8fdb\u884c\u5de6\u79fb\u64cd\u4f5c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u4e8c\u8fdb\u5236\u8ba1\u7b97\u7684\u5e38\u89c1\u9519\u8bef<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u4e8c\u8fdb\u5236\u8ba1\u7b97\u65f6\uff0c\u5bb9\u6613\u72af\u4e00\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u9700\u8981\u6ce8\u610f\u7684\u95ee\u9898\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u7b26\u53f7\u4f4d\u7684\u5904\u7406<\/li>\n<\/ol>\n<p><p>\u5728\u8fdb\u884c\u4e8c\u8fdb\u5236\u8ba1\u7b97\u65f6\uff0c\u7b26\u53f7\u4f4d\u7684\u5904\u7406\u9700\u8981\u7279\u522b\u6ce8\u610f\u3002\u4f8b\u5982\uff0c\u5728\u8fdb\u884c\u6309\u4f4d\u53d6\u53cd\u64cd\u4f5c\u65f6\uff0c\u7b26\u53f7\u4f4d\u4f1a\u53d1\u751f\u53d8\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10  # \u4e8c\u8fdb\u5236: 1010<\/p>\n<p>result = ~a  # \u8f93\u51fa: -11 (\u4e8c\u8fdb\u5236: 11111111111111111111111111110101)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u8fdb\u5236\u8f6c\u6362\u7684\u6b63\u786e\u6027<\/li>\n<\/ol>\n<p><p>\u5728\u8fdb\u884c\u8fdb\u5236\u8f6c\u6362\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u8f6c\u6362\u7684\u6b63\u786e\u6027\u3002\u4f8b\u5982\uff0c\u4f7f\u7528bin()\u51fd\u6570\u65f6\uff0c\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u5305\u542b\u524d\u7f00&#39;0b&#39;\uff0c\u9700\u8981\u53bb\u6389\u524d\u7f00\u4ee5\u83b7\u5f97\u7eaf\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>binary_str = bin(a)[2:]  # \u8f93\u51fa: &#39;1010&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u6570\u636e\u7c7b\u578b\u7684\u517c\u5bb9\u6027<\/li>\n<\/ol>\n<p><p>\u5728\u8fdb\u884c\u4e8c\u8fdb\u5236\u8ba1\u7b97\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u6570\u636e\u7c7b\u578b\u7684\u517c\u5bb9\u6027\u3002\u4f8b\u5982\uff0c\u5728\u4f7f\u7528numpy\u5e93\u65f6\uff0c\u9700\u8981\u6307\u5b9a\u6570\u636e\u7c7b\u578b\uff0c\u4ee5\u786e\u4fdd\u8ba1\u7b97\u7684\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>a = np.array([10], dtype=np.uint8)<\/p>\n<p>b = np.array([12], dtype=np.uint8)<\/p>\n<p>result = np.bitwise_and(a, b)  # \u8f93\u51fa: array([8], dtype=uint8)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516b\u3001\u4e8c\u8fdb\u5236\u8ba1\u7b97\u7684\u8fdb\u9636\u6280\u5de7<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u9ad8\u7ea7\u7684\u4e8c\u8fdb\u5236\u8ba1\u7b97\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u8fdb\u9636\u6280\u5de7\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u63a9\u7801\u6765\u9009\u62e9\u7279\u5b9a\u4f4d\uff0c\u6216\u8005\u4f7f\u7528\u4f4d\u57df\u6765\u538b\u7f29\u6570\u636e\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u63a9\u7801\u9009\u62e9\u7279\u5b9a\u4f4d<\/li>\n<\/ol>\n<p><p>\u63a9\u7801\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6280\u5de7\uff0c\u7528\u4e8e\u9009\u62e9\u7279\u5b9a\u4f4d\u3002\u4f8b\u5982\uff0c\u83b7\u53d6\u4e00\u4e2a\u6574\u6570\u7684\u6700\u4f4e\u4f4d\u6216\u6700\u9ad8\u4f4d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 0b1010<\/p>\n<h2><strong>\u83b7\u53d6\u6700\u4f4e\u4f4d<\/strong><\/h2>\n<p>lowest_bit = a &amp; 1  # \u8f93\u51fa: 0<\/p>\n<h2><strong>\u83b7\u53d6\u6700\u9ad8\u4f4d<\/strong><\/h2>\n<p>highest_bit = (a &gt;&gt; 3) &amp; 1  # \u8f93\u51fa: 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u4f4d\u57df\u538b\u7f29\u6570\u636e<\/li>\n<\/ol>\n<p><p>\u4f4d\u57df\u662f\u4e00\u79cd\u6570\u636e\u538b\u7f29\u6280\u5de7\uff0c\u901a\u8fc7\u5c06\u591a\u4e2a\u5b57\u6bb5\u538b\u7f29\u5230\u4e00\u4e2a\u6574\u6570\u4e2d\u6765\u8282\u7701\u5b58\u50a8\u7a7a\u95f4\u3002\u4f8b\u5982\uff0c\u5c06RGB\u989c\u8272\u503c\u538b\u7f29\u5230\u4e00\u4e2a\u6574\u6570\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06RGB\u989c\u8272\u503c\u538b\u7f29\u5230\u4e00\u4e2a\u6574\u6570\u4e2d<\/p>\n<p>r = 0b11111  # 5\u4f4d\u7ea2\u8272<\/p>\n<p>g = 0b111111  # 6\u4f4d\u7eff\u8272<\/p>\n<p>b = 0b11111  # 5\u4f4d\u84dd\u8272<\/p>\n<p>color = (r &lt;&lt; 11) | (g &lt;&lt; 5) | b  # \u8f93\u51fa: 0b111111111111111<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5d\u3001\u4e8c\u8fdb\u5236\u8ba1\u7b97\u7684\u8c03\u8bd5\u6280\u5de7<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u4e8c\u8fdb\u5236\u8ba1\u7b97\u65f6\uff0c\u8c03\u8bd5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u8c03\u8bd5\u6280\u5de7\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5370\u4e8c\u8fdb\u5236\u8868\u793a<\/li>\n<\/ol>\n<p><p>\u5728\u8c03\u8bd5\u65f6\uff0c\u53ef\u4ee5\u6253\u5370\u53d8\u91cf\u7684\u4e8c\u8fdb\u5236\u8868\u793a\uff0c\u4ee5\u4fbf\u66f4\u76f4\u89c2\u5730\u67e5\u770b\u8ba1\u7b97\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>binary_str = f&#39;{a:b}&#39;  # \u8f93\u51fa: &#39;1010&#39;<\/p>\n<p>print(f&#39;Binary representation of {a}: {binary_str}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u65ad\u70b9\u8c03\u8bd5<\/li>\n<\/ol>\n<p><p>\u5728\u8c03\u8bd5\u590d\u6742\u7684\u4e8c\u8fdb\u5236\u8ba1\u7b97\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u65ad\u70b9\u8c03\u8bd5\u5de5\u5177\uff0c\u5982Python\u7684pdb\u6a21\u5757\uff0c\u6765\u9010\u6b65\u68c0\u67e5\u53d8\u91cf\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pdb<\/p>\n<p>a = 10<\/p>\n<p>b = 12<\/p>\n<p>pdb.set_trace()  # \u5728\u8fd9\u91cc\u8bbe\u7f6e\u65ad\u70b9<\/p>\n<p>result = a &amp; b<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u4f7f\u7528\u5355\u5143\u6d4b\u8bd5<\/li>\n<\/ol>\n<p><p>\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u81ea\u52a8\u5316\u6d4b\u8bd5\u4e8c\u8fdb\u5236\u8ba1\u7b97\u7684\u6b63\u786e\u6027\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684unittest\u6a21\u5757\u6765\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unittest<\/p>\n<p>class TestBinaryCalculation(unittest.TestCase):<\/p>\n<p>    def test_bitwise_and(self):<\/p>\n<p>        self.assertEqual(10 &amp; 12, 8)<\/p>\n<p>    def test_bitwise_or(self):<\/p>\n<p>        self.assertEqual(10 | 12, 14)<\/p>\n<p>    def test_bitwise_xor(self):<\/p>\n<p>        self.assertEqual(10 ^ 12, 6)<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5341\u3001\u4e8c\u8fdb\u5236\u8ba1\u7b97\u7684\u5b9e\u6218\u6848\u4f8b<\/p>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\u6765\u5c55\u793a\u5982\u4f55\u8fdb\u884c\u4e8c\u8fdb\u5236\u8ba1\u7b97\u3002\u5047\u8bbe\u6211\u4eec\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\u6765\u8ba1\u7b97\u6587\u4ef6\u7684\u6821\u9a8c\u548c\uff0c\u4ee5\u68c0\u6d4b\u6587\u4ef6\u4f20\u8f93\u4e2d\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_checksum(file_path):<\/p>\n<p>    checksum = 0<\/p>\n<p>    with open(file_path, &#39;rb&#39;) as f:<\/p>\n<p>        while byte := f.read(1):<\/p>\n<p>            checksum ^= int.from_bytes(byte, byteorder=&#39;big&#39;)<\/p>\n<p>    return checksum<\/p>\n<h2><strong>\u8ba1\u7b97\u6587\u4ef6\u7684\u6821\u9a8c\u548c<\/strong><\/h2>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>checksum = calculate_checksum(file_path)<\/p>\n<p>print(f&#39;Checksum: {checksum}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u6848\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6309\u4f4d\u5f02\u6216\u64cd\u4f5c\u6765\u8ba1\u7b97\u6587\u4ef6\u7684\u6821\u9a8c\u548c\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u68c0\u6d4b\u6587\u4ef6\u4f20\u8f93\u4e2d\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86Python\u4e2d\u8fdb\u884c\u4e8c\u8fdb\u5236\u8ba1\u7b97\u7684\u5404\u79cd\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u4f4d\u8fd0\u7b97\u7b26\u3001\u683c\u5f0f\u5316\u8f93\u51fa\u3001\u5e93\u51fd\u6570\u7b49\u3002\u6211\u4eec\u8fd8\u8ba8\u8bba\u4e86\u4e8c\u8fdb\u5236\u8ba1\u7b97\u7684\u5b9e\u9645\u5e94\u7528\u3001\u6027\u80fd\u4f18\u5316\u3001\u5e38\u89c1\u9519\u8bef\u3001\u8fdb\u9636\u6280\u5de7\u3001\u8c03\u8bd5\u6280\u5de7\u548c\u5b9e\u6218\u6848\u4f8b\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u5728\u8fdb\u884c\u4e8c\u8fdb\u5236\u8ba1\u7b97\u65f6\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\u6570\u7684\u8f6c\u6362\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u5185\u7f6e\u51fd\u6570<code>bin()<\/code>\uff0c\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6574\u6570\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c<code>bin(5)<\/code>\u4f1a\u8fd4\u56de<code>&#39;0b101&#39;<\/code>\u3002\u5982\u679c\u60a8\u60f3\u53bb\u6389\u524d\u7f00<code>0b<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247<code>bin(5)[2:]<\/code>\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u5c06\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u4f8b\u5982<code>int(&#39;101&#39;, 2)<\/code>\u4f1a\u8fd4\u56de<code>5<\/code>\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u8fdb\u884c\u4e8c\u8fdb\u5236\u52a0\u6cd5\u548c\u51cf\u6cd5\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>+<\/code>\u548c<code>-<\/code>\u8fd0\u7b97\u7b26\u5bf9\u6574\u6570\u8fdb\u884c\u52a0\u6cd5\u548c\u51cf\u6cd5\uff0c\u7136\u540e\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5f62\u5f0f\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<code>result = bin(5 + 3)<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de<code>&#39;0b1000&#39;<\/code>\u3002\u5982\u679c\u9700\u8981\u5904\u7406\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u5148\u5c06\u5b83\u4eec\u8f6c\u6362\u4e3a\u6574\u6570\u8fdb\u884c\u8fd0\u7b97\uff0c\u7136\u540e\u518d\u8f6c\u6362\u56de\u4e8c\u8fdb\u5236\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u8fdb\u884c\u4f4d\u8fd0\u7b97\uff1f<\/strong><br \/>Python\u652f\u6301\u591a\u79cd\u4f4d\u8fd0\u7b97\u7b26\uff0c\u5305\u62ec\u4e0e<code>&amp;<\/code>\u3001\u6216<code>|<\/code>\u3001\u5f02\u6216<code>^<\/code>\u3001\u53d6\u53cd<code>~<\/code>\u3001\u5de6\u79fb<code>&lt;&lt;<\/code>\u548c\u53f3\u79fb<code>&gt;&gt;<\/code>\u3002\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u6574\u6570\u3002\u4f8b\u5982\uff0c<code>a &amp; b<\/code>\u4f1a\u8fd4\u56dea\u548cb\u7684\u6309\u4f4d\u4e0e\u7ed3\u679c\u3002\u5982\u679c\u60a8\u60f3\u5bf9\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u8fdb\u884c\u4f4d\u8fd0\u7b97\uff0c\u9700\u5148\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u8fd0\u7b97\u540e\u518d\u8f6c\u6362\u56de\u4e8c\u8fdb\u5236\u683c\u5f0f\u3002\u4f4d\u8fd0\u7b97\u5728\u5904\u7406\u4f4e\u7ea7\u6570\u636e\u65f6\u975e\u5e38\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u9700\u8981\u76f4\u63a5\u64cd\u4f5c\u6570\u636e\u4f4d\u7684\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d: \u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u4f4d\u8fd0\u7b97\u7b26\u3001\u683c\u5f0f\u5316\u8f93\u51fa\u3001\u5e93\u51fd\u6570\u7b49\u662fPython\u4e2d\u8fdb\u884c\u4e8c\u8fdb\u5236\u8ba1\u7b97\u7684\u4e3b\u8981\u65b9\u5f0f\u3002\u4f4d\u8fd0\u7b97\u7b26\u662f\u5176 [&hellip;]","protected":false},"author":3,"featured_media":1036497,"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\/1036491"}],"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=1036491"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1036491\/revisions"}],"predecessor-version":[{"id":1036503,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1036491\/revisions\/1036503"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1036497"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1036491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1036491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1036491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}