{"id":3419,"date":"2025-04-01T23:32:15","date_gmt":"2025-04-01T15:32:15","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3419"},"modified":"2025-04-01T23:32:15","modified_gmt":"2025-04-01T15:32:15","slug":"c-%e4%bd%8d%e5%9f%9f%ef%bc%88bit-field%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/01\/c-%e4%bd%8d%e5%9f%9f%ef%bc%88bit-field%ef%bc%89\/","title":{"rendered":"C \u4f4d\u57df\uff08Bit-field\uff09"},"content":{"rendered":"\n<p>\u5728 C \u8bed\u8a00\u4e2d\uff0c<strong>\u4f4d\u57df\uff08bit-field\uff09<\/strong> \u662f\u7ed3\u6784\u4f53\u7684\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u7ed3\u6784\u4f53\u4e2d\u5b9a\u4e49\u5360\u7528\u56fa\u5b9a\u6570\u91cf\u7684\u4f4d\u6570\u7684\u6210\u5458\u3002\u4f4d\u57df\u7528\u4e8e\u5904\u7406\u8981\u6c42\u5360\u7528\u7279\u5b9a\u6570\u91cf\u6bd4\u7279\uff08bit\uff09\u7684\u5c0f\u6570\u636e\u7c7b\u578b\uff0c\u901a\u5e38\u7528\u4e8e\u786c\u4ef6\u7f16\u7a0b\u3001\u534f\u8bae\u6570\u636e\u5904\u7406\u6216\u5176\u4ed6\u5185\u5b58\u53d7\u9650\u7684\u5e94\u7528\u4e2d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u4f4d\u57df\u7684\u57fa\u672c\u5b9a\u4e49<\/strong><\/h3>\n\n\n\n<p>\u4f4d\u57df\u5728\u7ed3\u6784\u4f53\u4e2d\u5b9a\u4e49\u65f6\uff0c\u6307\u5b9a\u6210\u5458\u6240\u5360\u7684\u6bd4\u7279\u4f4d\u6570\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct StructName {\n    dataType memberName : bitCount;\n};<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>dataType<\/code> \u662f\u6210\u5458\u7684\u7c7b\u578b\uff08\u901a\u5e38\u662f <code>int<\/code>, <code>short<\/code>, <code>char<\/code> \u7b49\uff09\uff0c<\/li>\n\n\n\n<li><code>memberName<\/code> \u662f\u6210\u5458\u7684\u540d\u5b57\uff0c<\/li>\n\n\n\n<li><code>bitCount<\/code> \u662f\u6210\u5458\u6240\u5360\u7528\u7684\u6bd4\u7279\u4f4d\u6570\uff0c\u901a\u5e38\u662f 1 \u5230\u8be5\u6570\u636e\u7c7b\u578b\u7684\u6700\u5927\u4f4d\u6570\uff08\u6bd4\u5982 <code>int<\/code> \u901a\u5e38\u5360 32 \u4f4d\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. \u4f4d\u57df\u7684\u4f8b\u5b50<\/strong><\/h3>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u4f4d\u57df\u7684\u7b80\u5355\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nstruct BitField {\n    unsigned int a : 3;  \/\/ \u5360\u7528 3 \u4f4d\n    unsigned int b : 5;  \/\/ \u5360\u7528 5 \u4f4d\n    unsigned int c : 8;  \/\/ \u5360\u7528 8 \u4f4d\n};\n\nint main() {\n    struct BitField bf = {5, 10, 255};  \/\/ \u521d\u59cb\u5316\u7ed3\u6784\u4f53\n\n    printf(\"a = %u\\n\", bf.a);  \/\/ \u8f93\u51fa a\n    printf(\"b = %u\\n\", bf.b);  \/\/ \u8f93\u51fa b\n    printf(\"c = %u\\n\", bf.c);  \/\/ \u8f93\u51fa c\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u8f93\u51fa\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 5\nb = 10\nc = 255<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. \u4f4d\u57df\u7684\u7279\u70b9<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5185\u5b58\u7d27\u51d1<\/strong>\uff1a\u4f4d\u57df\u5141\u8bb8\u5c06\u7ed3\u6784\u4f53\u6210\u5458\u9650\u5236\u4e3a\u7279\u5b9a\u7684\u6bd4\u7279\u6570\uff0c\u8fd9\u4f7f\u5f97\u5185\u5b58\u4f7f\u7528\u66f4\u52a0\u7d27\u51d1\u3002<\/li>\n\n\n\n<li><strong>\u6570\u636e\u7c7b\u578b\u9650\u5236<\/strong>\uff1a\u4f4d\u57df\u901a\u5e38\u662f\u6574\u6570\u7c7b\u578b\uff08\u5982 <code>int<\/code>, <code>unsigned int<\/code>\uff09\u3002\u4f7f\u7528\u4f4d\u57df\u65f6\uff0c\u5b57\u6bb5\u7684\u5927\u5c0f\u7531\u5b9a\u4e49\u65f6\u7684\u4f4d\u6570\u51b3\u5b9a\uff0c\u56e0\u6b64\u4e0d\u80fd\u8d85\u8fc7\u8be5\u6570\u636e\u7c7b\u578b\u7684\u5927\u5c0f\u3002<\/li>\n\n\n\n<li><strong>\u5185\u5b58\u5bf9\u9f50<\/strong>\uff1a\u4f4d\u57df\u7684\u5185\u5b58\u5e03\u5c40\u4f1a\u53d7\u5230\u5185\u5b58\u5bf9\u9f50\u89c4\u5219\u7684\u5f71\u54cd\u3002\u7ed3\u6784\u4f53\u7684\u5176\u4ed6\u6210\u5458\u53ef\u80fd\u4f1a\u63d2\u5165\u586b\u5145\u5b57\u8282\uff0c\u4ee5\u786e\u4fdd\u6210\u5458\u6309\u6b63\u786e\u7684\u8fb9\u754c\u5bf9\u9f50\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. \u4f4d\u57df\u7684\u4f7f\u7528<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u786c\u4ef6\u7f16\u7a0b<\/strong>\uff1a\u4f4d\u57df\u53ef\u4ee5\u7528\u4e8e\u786c\u4ef6\u7f16\u7a0b\uff0c\u76f4\u63a5\u8bbf\u95ee\u7279\u5b9a\u7684\u786c\u4ef6\u5bc4\u5b58\u5668\u548c\u63a7\u5236\u4f4d\u3002<\/li>\n\n\n\n<li><strong>\u534f\u8bae\u89e3\u6790<\/strong>\uff1a\u7f51\u7edc\u534f\u8bae\u6216\u6570\u636e\u5305\u7684\u683c\u5f0f\u901a\u5e38\u8981\u6c42\u6309\u4f4d\u5b58\u50a8\u4fe1\u606f\uff0c\u4f4d\u57df\u7279\u522b\u9002\u7528\u4e8e\u6b64\u7c7b\u4efb\u52a1\u3002<\/li>\n\n\n\n<li><strong>\u5185\u5b58\u4f18\u5316<\/strong>\uff1a\u5f53\u5185\u5b58\u7a7a\u95f4\u6709\u9650\u65f6\uff0c\u901a\u8fc7\u4f4d\u57df\u5c06\u6570\u636e\u538b\u7f29\u5230\u6307\u5b9a\u7684\u4f4d\u6570\uff0c\u53ef\u4ee5\u51cf\u5c11\u5185\u5b58\u6d88\u8017\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. \u6ce8\u610f\u4e8b\u9879<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7c7b\u578b\u9650\u5236<\/strong>\uff1a\u4f4d\u57df\u7684\u7c7b\u578b\u901a\u5e38\u662f\u6574\u6570\u7c7b\u578b\uff08<code>int<\/code> \u6216 <code>unsigned int<\/code>\uff09\uff0c\u5176\u4ed6\u7c7b\u578b\uff08\u5982\u6d6e\u70b9\u6570\u6216\u5b57\u7b26\uff09\u4e0d\u80fd\u7528\u4e8e\u4f4d\u57df\u3002<\/li>\n\n\n\n<li><strong>\u4f4d\u6570\u9650\u5236<\/strong>\uff1a\u4f4d\u57df\u7684\u4f4d\u6570\u5e94\u8be5\u5c0f\u4e8e\u6216\u7b49\u4e8e\u6240\u4f7f\u7528\u7684\u6570\u636e\u7c7b\u578b\u7684\u4f4d\u6570\u3002\u6bd4\u5982\uff0c<code>int<\/code> \u901a\u5e38\u6709 32 \u4f4d\uff0c\u6700\u5927\u4f4d\u6570\u4e3a 32\u3002<\/li>\n\n\n\n<li><strong>\u5b58\u50a8\u987a\u5e8f\u4e0e\u5185\u5b58\u5bf9\u9f50<\/strong>\uff1a\u4f4d\u57df\u7684\u5b58\u50a8\u987a\u5e8f\u53ef\u80fd\u4f1a\u53d7\u5230\u5e73\u53f0\u7684\u5f71\u54cd\uff0c\u901a\u5e38\u6309\u7167\u4ece\u5de6\u5230\u53f3\u7684\u987a\u5e8f\u5b58\u50a8\uff0c\u4f46\u5728\u67d0\u4e9b\u5e73\u53f0\u4e0a\uff0c\u53ef\u80fd\u5b58\u5728\u5185\u5b58\u5bf9\u9f50\u6216\u5b57\u8282\u987a\u5e8f\uff08\u5927\u7aef\/\u5c0f\u7aef\uff09\u7684\u95ee\u9898\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. \u4f4d\u57df\u7684\u5b9e\u9645\u5e94\u7528<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>6.1 \u5b58\u50a8\u4e0d\u540c\u72b6\u6001\u7684\u6807\u5fd7\u4f4d<\/strong><\/h4>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528\u4f4d\u57df\u8868\u793a\u72b6\u6001\u6807\u5fd7\uff0c\u6bcf\u4e2a\u72b6\u6001\u4ec5\u5360\u7528\u4e00\u4e2a\u6bd4\u7279\u3002\u4f8b\u5982\uff0c\u8868\u793a\u8bbe\u5907\u7684\u4e0d\u540c\u72b6\u6001\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nstruct DeviceStatus {\n    unsigned int is_on : 1;      \/\/ \u8bbe\u5907\u662f\u5426\u5f00\u542f\n    unsigned int is_error : 1;   \/\/ \u662f\u5426\u6709\u9519\u8bef\n    unsigned int is_locked : 1;  \/\/ \u662f\u5426\u9501\u5b9a\n    unsigned int reserved : 5;   \/\/ \u4fdd\u7559\u4f4d\n};\n\nint main() {\n    struct DeviceStatus status = {1, 0, 1};  \/\/ \u8bbe\u7f6e\u8bbe\u5907\u72b6\u6001\n\n    printf(\"Device is on: %u\\n\", status.is_on);\n    printf(\"Device has error: %u\\n\", status.is_error);\n    printf(\"Device is locked: %u\\n\", status.is_locked);\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>6.2 \u5904\u7406\u534f\u8bae\u5934\u4fe1\u606f<\/strong><\/h4>\n\n\n\n<p>\u5728\u7f51\u7edc\u901a\u4fe1\u534f\u8bae\u4e2d\uff0c\u6570\u636e\u5305\u7684\u5b57\u6bb5\u53ef\u80fd\u5f88\u5c0f\uff0c\u4f4d\u57df\u80fd\u5e2e\u52a9\u6709\u6548\u5730\u5b58\u50a8\u548c\u8bbf\u95ee\u8fd9\u4e9b\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nstruct PacketHeader {\n    unsigned int version : 4;  \/\/ 4 bits for version\n    unsigned int type : 4;     \/\/ 4 bits for type\n    unsigned int length : 8;   \/\/ 8 bits for length\n    unsigned int checksum : 16; \/\/ 16 bits for checksum\n};\n\nint main() {\n    struct PacketHeader packet = {1, 2, 255, 12345};\n\n    printf(\"Version: %u\\n\", packet.version);\n    printf(\"Type: %u\\n\", packet.type);\n    printf(\"Length: %u\\n\", packet.length);\n    printf(\"Checksum: %u\\n\", packet.checksum);\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>6.3 \u4f4d\u57df\u7684\u538b\u7f29\u5b58\u50a8<\/strong><\/h4>\n\n\n\n<p>\u4f4d\u57df\u80fd\u591f\u6709\u6548\u5730\u538b\u7f29\u591a\u4e2a\u5e03\u5c14\u503c\u6216\u5c0f\u6574\u6570\u503c\uff0c\u4ece\u800c\u8282\u7701\u5185\u5b58\uff0c\u5c24\u5176\u5728\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7. \u4f4d\u57df\u7684\u5185\u5b58\u5bf9\u9f50<\/strong><\/h3>\n\n\n\n<p>\u4f4d\u57df\u7684\u5185\u5b58\u5e03\u5c40\u53ef\u80fd\u53d7\u5230\u5bf9\u9f50\u8981\u6c42\u7684\u5f71\u54cd\uff0c\u53ef\u80fd\u4f1a\u5728\u7ed3\u6784\u4f53\u6210\u5458\u4e4b\u95f4\u63d2\u5165\u586b\u5145\u5b57\u8282\u3002\u4e0d\u540c\u5e73\u53f0\u4e0a\u7684\u5b57\u8282\u5bf9\u9f50\u89c4\u5219\u4e5f\u53ef\u80fd\u6709\u6240\u4e0d\u540c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nstruct BitField {\n    unsigned int a : 5;\n    unsigned int b : 5;\n    unsigned int c : 6;\n    unsigned int d : 8;\n};\n\nint main() {\n    printf(\"Size of struct: %lu\\n\", sizeof(struct BitField));\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u8f93\u51fa\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Size of struct: 4<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5c3d\u7ba1\u7ed3\u6784\u4f53\u7684\u603b\u4f4d\u6570\u662f 24 \u4f4d\uff085 + 5 + 6 + 8\uff09\uff0c\u4f46\u56e0\u4e3a\u5185\u5b58\u5bf9\u9f50\u7684\u539f\u56e0\uff0c\u5b9e\u9645\u7684\u5927\u5c0f\u53ef\u80fd\u662f 4 \u5b57\u8282\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8. \u53c2\u8003\u8d44\u6599<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.runoob.com\/cprogramming\/c-bit-field.html\">C \u4f4d\u57df\u6559\u7a0b &#8211; \u83dc\u9e1f\u6559\u7a0b<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.open-std.org\/jtc1\/sc22\/wg14\/www\/docs\/n2310.pdf\">ISO C Standard &#8211; Bit-field<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4f4d\u57df\u975e\u5e38\u9002\u5408\u5904\u7406\u5d4c\u5165\u5f0f\u7cfb\u7edf\u6216\u5176\u4ed6\u5185\u5b58\u53d7\u9650\u7684\u5e94\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 C \u8bed\u8a00\u4e2d\uff0c\u4f4d\u57df\uff08bit-field\uff09 \u662f\u7ed3\u6784\u4f53\u7684\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u7ed3\u6784\u4f53\u4e2d\u5b9a\u4e49\u5360\u7528\u56fa\u5b9a\u6570\u91cf\u7684\u4f4d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3420,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"class_list":["post-3419","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3419"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3419\/revisions"}],"predecessor-version":[{"id":3421,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3419\/revisions\/3421"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3420"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}