{"id":17688,"date":"2024-11-18T17:20:20","date_gmt":"2024-11-18T09:20:20","guid":{"rendered":"https:\/\/fwq.ai\/blog\/17688\/"},"modified":"2024-11-18T17:20:20","modified_gmt":"2024-11-18T09:20:20","slug":"linux-c%e8%af%ad%e8%a8%80%e4%b8%ad%e7%9a%84%e4%bd%8d%e6%93%8d%e4%bd%9c%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/17688\/","title":{"rendered":"Linux C\u8bed\u8a00\u4e2d\u7684\u4f4d\u64cd\u4f5c\u6280\u5de7"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/171041076673211.jpg\" class=\"aligncenter\" title=\"Linux C\u8bed\u8a00\u4e2d\u7684\u4f4d\u64cd\u4f5c\u6280\u5de7\u63d2\u56fe\" alt=\"Linux C\u8bed\u8a00\u4e2d\u7684\u4f4d\u64cd\u4f5c\u6280\u5de7\u63d2\u56fe\" \/><\/p>\n<p>\u6807\u9898\uff1a\u6df1\u5165\u63a2\u8ba8Linux C\u8bed\u8a00\u4e2d\u7684\u4f4d\u64cd\u4f5c\u6280\u5de7<\/p>\n<p>\u5728Linux\u7cfb\u7edf\u4e0b\u4f7f\u7528C\u8bed\u8a00\u8fdb\u884c\u5f00\u53d1\u65f6\uff0c\u4f4d\u64cd\u4f5c\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u4e14\u7ecf\u5e38\u4f7f\u7528\u7684\u6280\u5de7\u3002\u901a\u8fc7\u4f4d\u64cd\u4f5c\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u6548\u7684\u6570\u636e\u5904\u7406\u3001\u4f18\u5316\u7b97\u6cd5\u3001\u8282\u7701\u5185\u5b58\u7b49\u529f\u80fd\u3002\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8\u5728Linux\u73af\u5883\u4e0b\u4f7f\u7528C\u8bed\u8a00\u8fdb\u884c\u4f4d\u64cd\u4f5c\u7684\u6280\u5de7\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<h3>1. \u4f4d\u64cd\u4f5c\u6280\u5de7\u6982\u8ff0<\/h3>\n<p>\u4f4d\u64cd\u4f5c\u662f\u5bf9\u6574\u6570\u7c7b\u578b\u7684\u6570\u636e\u5728\u6bd4\u7279\u7ea7\u522b\u4e0a\u8fdb\u884c\u64cd\u4f5c\u7684\u8fc7\u7a0b\u3002\u5728C\u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f4d\u64cd\u4f5c\u7b26\uff08&amp;\u3001|\u3001^\u3001~\u3001&gt;\uff09\u6765\u5b9e\u73b0\u4f4d\u64cd\u4f5c\u3002\u901a\u8fc7\u4f4d\u64cd\u4f5c\uff0c\u6211\u4eec\u53ef\u4ee5\u5b8c\u6210\u8bf8\u5982\u4f4d\u4e0e\u3001\u4f4d\u6216\u3001\u4f4d\u5f02\u6216\u3001\u4f4d\u53d6\u53cd\u3001\u5de6\u79fb\u4f4d\u3001\u53f3\u79fb\u4f4d\u7b49\u64cd\u4f5c\u3002<\/p>\n<h3>2. \u6e05\u9664\u6574\u6570\u7684\u7279\u5b9a\u4f4d<\/h3>\n<p>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u5c06\u6574\u6570\u7684\u7279\u5b9a\u4f4d\u6e05\u96f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4f4d\u4e0e\u64cd\u4f5c\u7b26&amp;\u548c\u4f4d\u53d6\u53cd\u64cd\u4f5c\u7b26~\u6765\u5b9e\u73b0\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c06\u6574\u6570num\u7684\u7b2cn\u4f4d\u6e05\u96f6\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>unsigned int clearBit(unsigned int num, int n) {\n    unsigned int mask = ~(1 &lt;h3&gt;3. \u8bbe\u7f6e\u6574\u6570\u7684\u7279\u5b9a\u4f4d&lt;\/h3&gt;&lt;p&gt;\u7c7b\u4f3c\u5730\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u8bbe\u7f6e\u6574\u6570\u7684\u7279\u5b9a\u4f4d\u4e3a1\uff0c\u53ef\u4ee5\u4f7f\u7528\u4f4d\u6216\u64cd\u4f5c\u7b26|\u548c\u5de6\u79fb\u4f4d\u64cd\u4f5c\u7b26&lt;\/p&gt;&lt;pre class=\"brush:c;toolbar:false;\"&gt;unsigned int setBit(unsigned int num, int n) {\n    unsigned int mask = 1 &lt;h3&gt;4. \u5207\u6362\u6574\u6570\u7684\u7279\u5b9a\u4f4d&lt;\/h3&gt;&lt;p&gt;\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u5207\u6362\u6574\u6570\u7684\u7279\u5b9a\u4f4d\uff0c\u5373\u5982\u679c\u8be5\u4f4d\u4e3a0\u5219\u6539\u4e3a1\uff0c\u5982\u679c\u8be5\u4f4d\u4e3a1\u5219\u6539\u4e3a0\u3002\u53ef\u4ee5\u4f7f\u7528\u4f4d\u5f02\u6216\u64cd\u4f5c\u7b26^\u6765\u5b9e\u73b0\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5207\u6362\u6574\u6570num\u7684\u7b2cn\u4f4d\uff1a&lt;\/p&gt;&lt;pre class=\"brush:c;toolbar:false;\"&gt;unsigned int toggleBit(unsigned int num, int n) {\n    unsigned int mask = 1 &lt;h3&gt;5. \u68c0\u6d4b\u6574\u6570\u7684\u7279\u5b9a\u4f4d\u662f\u5426\u4e3a1&lt;\/h3&gt;&lt;p&gt;\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4f4d\u4e0e\u64cd\u4f5c\u7b26&amp;amp;\u6765\u68c0\u6d4b\u6574\u6570\u7684\u7279\u5b9a\u4f4d\u662f\u5426\u4e3a1\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u68c0\u6d4b\u6574\u6570num\u7684\u7b2cn\u4f4d\u662f\u5426\u4e3a1\uff1a&lt;\/p&gt;&lt;pre class=\"brush:c;toolbar:false;\"&gt;int isBitSet(unsigned int num, int n) {\n    unsigned int mask = 1 &lt;h3&gt;6. \u5c06\u6574\u6570\u7684\u7279\u5b9a\u4f4d\u53f3\u79fb\u81f3\u6700\u4f4e\u4f4d&lt;\/h3&gt;&lt;p&gt;\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u5c06\u6574\u6570\u7684\u7279\u5b9a\u4f4d\u53f3\u79fb\u81f3\u6700\u4f4e\u4f4d\uff0c\u53ef\u4ee5\u4f7f\u7528\u53f3\u79fb\u4f4d\u64cd\u4f5c\u7b26&amp;gt;&amp;gt;\u6765\u5b9e\u73b0\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c06\u6574\u6570num\u7684\u7b2cn\u4f4d\u53f3\u79fb\u81f3\u6700\u4f4e\u4f4d\uff1a&lt;\/p&gt;&lt;pre class=\"brush:c;toolbar:false;\"&gt;unsigned int moveBitRight(unsigned int num, int n) {\n    return (num &amp;gt;&amp;gt; n) &amp;amp; 1;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3>\u7ed3\u8bed<\/h3>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u7684\u4f4d\u64cd\u4f5c\u6280\u5de7\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5728Linux\u73af\u5883\u4e0b\u4f7f\u7528C\u8bed\u8a00\u8fdb\u884c\u4f4d\u64cd\u4f5c\u662f\u591a\u4e48\u7684\u91cd\u8981\u548c\u5b9e\u7528\u3002\u4f4d\u64cd\u4f5c\u4e0d\u4ec5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u9ad8\u6548\u5730\u5904\u7406\u6570\u636e\uff0c\u8fd8\u53ef\u4ee5\u4f18\u5316\u7b97\u6cd5\u7684\u6027\u80fd\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u8bfb\u8005\u6709\u6240\u542f\u53d1\uff0c\u80fd\u591f\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u7075\u6d3b\u8fd0\u7528\u4f4d\u64cd\u4f5c\u6280\u5de7\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fLinux C\u8bed\u8a00\u4e2d\u7684\u4f4d\u64cd\u4f5c\u6280\u5de7\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6807\u9898\uff1a\u6df1\u5165\u63a2\u8ba8Linux C\u8bed\u8a00\u4e2d\u7684\u4f4d\u64cd\u4f5c\u6280\u5de7 \u5728Linux\u7cfb\u7edf\u4e0b\u4f7f\u7528C\u8bed\u8a00\u8fdb\u884c\u5f00\u53d1\u65f6\uff0c\u4f4d\u64cd\u4f5c\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u4e14\u7ecf\u5e38\u4f7f\u7528\u7684\u6280\u5de7\u3002\u901a\u8fc7\u4f4d\u64cd\u4f5c\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u6548\u7684\u6570\u636e\u5904\u7406\u3001\u4f18\u5316\u7b97\u6cd5\u3001\u8282\u7701\u5185\u5b58\u7b49\u529f\u80fd\u3002\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8\u5728Linux\u73af\u5883\u4e0b\u4f7f\u7528C\u8bed\u8a00\u8fdb\u884c\u4f4d\u64cd\u4f5c\u7684\u6280\u5de7\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002 1. \u4f4d\u64cd\u4f5c\u6280\u5de7\u6982\u8ff0 \u4f4d\u64cd\u4f5c\u662f\u5bf9\u6574\u6570\u7c7b\u578b\u7684\u6570\u636e\u5728\u6bd4\u7279\u7ea7\u522b\u4e0a\u8fdb\u884c\u64cd\u4f5c\u7684\u8fc7\u7a0b\u3002\u5728C\u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f4d\u64cd\u4f5c\u7b26\uff08&amp;\u3001|\u3001^\u3001~\u3001&gt;\uff09\u6765\u5b9e\u73b0\u4f4d\u64cd\u4f5c\u3002\u901a\u8fc7\u4f4d\u64cd\u4f5c\uff0c\u6211\u4eec\u53ef\u4ee5\u5b8c\u6210\u8bf8\u5982\u4f4d\u4e0e\u3001\u4f4d\u6216\u3001\u4f4d\u5f02\u6216\u3001\u4f4d\u53d6\u53cd\u3001\u5de6\u79fb\u4f4d\u3001\u53f3\u79fb\u4f4d\u7b49\u64cd\u4f5c\u3002 2. \u6e05\u9664\u6574\u6570\u7684\u7279\u5b9a\u4f4d \u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u5c06\u6574\u6570\u7684\u7279\u5b9a\u4f4d\u6e05\u96f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4f4d\u4e0e\u64cd\u4f5c\u7b26&amp;\u548c\u4f4d\u53d6\u53cd\u64cd\u4f5c\u7b26~\u6765\u5b9e\u73b0\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c06\u6574\u6570num\u7684\u7b2cn\u4f4d\u6e05\u96f6\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b unsigned int clearBit(unsigned int num, int n) { unsigned int mask = ~(1 &lt;h3&gt;3. \u8bbe\u7f6e\u6574\u6570\u7684\u7279\u5b9a\u4f4d&lt;\/h3&gt;&lt;p&gt;\u7c7b\u4f3c\u5730\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u8bbe\u7f6e\u6574\u6570\u7684\u7279\u5b9a\u4f4d\u4e3a1\uff0c\u53ef\u4ee5\u4f7f\u7528\u4f4d\u6216\u64cd\u4f5c\u7b26|\u548c\u5de6\u79fb\u4f4d\u64cd\u4f5c\u7b26&lt;\/p&gt;&lt;pre class=&#8221;brush:c;toolbar:false;&#8221;&gt;unsigned int setBit(unsigned int num, int n) { unsigned int mask = 1 &lt;h3&gt;4. \u5207\u6362\u6574\u6570\u7684\u7279\u5b9a\u4f4d&lt;\/h3&gt;&lt;p&gt;\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u5207\u6362\u6574\u6570\u7684\u7279\u5b9a\u4f4d\uff0c\u5373\u5982\u679c\u8be5\u4f4d\u4e3a0\u5219\u6539\u4e3a1\uff0c\u5982\u679c\u8be5\u4f4d\u4e3a1\u5219\u6539\u4e3a0\u3002\u53ef\u4ee5\u4f7f\u7528\u4f4d\u5f02\u6216\u64cd\u4f5c\u7b26^\u6765\u5b9e\u73b0\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5207\u6362\u6574\u6570num\u7684\u7b2cn\u4f4d\uff1a&lt;\/p&gt;&lt;pre class=&#8221;brush:c;toolbar:false;&#8221;&gt;unsigned int toggleBit(unsigned int num, int n) { unsigned int mask = 1 &lt;h3&gt;5. \u68c0\u6d4b\u6574\u6570\u7684\u7279\u5b9a\u4f4d\u662f\u5426\u4e3a1&lt;\/h3&gt;&lt;p&gt;\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4f4d\u4e0e\u64cd\u4f5c\u7b26&amp;amp;\u6765\u68c0\u6d4b\u6574\u6570\u7684\u7279\u5b9a\u4f4d\u662f\u5426\u4e3a1\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u68c0\u6d4b\u6574\u6570num\u7684\u7b2cn\u4f4d\u662f\u5426\u4e3a1\uff1a&lt;\/p&gt;&lt;pre [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-17688","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/17688","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=17688"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/17688\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=17688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=17688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=17688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}