{"id":4699,"date":"2022-02-08T11:08:50","date_gmt":"2022-02-08T02:08:50","guid":{"rendered":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4699"},"modified":"2022-02-08T16:18:34","modified_gmt":"2022-02-08T07:18:34","slug":"c-struct-bit-field","status":"publish","type":"post","link":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4699","title":{"rendered":"C &#8211; \uad6c\uc870\uccb4 \ube44\ud2b8 \ud544\ub4dc(struct bit field)"},"content":{"rendered":"<h1>C &#8211; \uad6c\uc870\uccb4 \ube44\ud2b8 \ud544\ub4dc(struct bit field)<\/h1>\n<p><a href=\"https:\/\/www.it-note.kr\/m\/312\">\ucc38\uc870<\/a><\/p>\n<p>. . . . . . . . . . . . . . . . . . . . . . . . .<br \/>\n. . . . . . . . . . . . . . . . . . . . . . . . .<br \/>\n. . . . . . . . . . . . . . . . . . . . . . . . .<\/p>\n<h2>\uae30\ubcf8 \ubb38\ubc95<\/h2>\n<p>\uc815\uc218\ud615\uc5d0\ub294 char, short, int, long \uc774 \uc62c \uc218 \uc788\ub2e4.<\/p>\n<pre><code class=\"language-c\">struct \uad6c\uc870\uccb4\uba85 {\n    unsigned \uc815\uc218\ud615 \uba64\ubc84\ud544\ub4dc1 : \ube44\ud2b8\uc218;\n    unsigned \uc815\uc218\ud615 \uba64\ubc84\ud544\ub4dc2 : \ube44\ud2b8\uc218;\n    ...\n};\n\n\/\/ \uc608\uc81c\nstruct file_access {\n    unsigned short other     : 3; \/\/ rwx\n    unsigned short group     : 3; \/\/ rwx\n    unsigned short owner     : 3; \/\/ rwx\n    unsigned short type      : 3;\n    unsigned short egroup    : 1;\n    unsigned short euser     : 1;\n};<\/code><\/pre>\n<h2>\uad6c\uc870\uccb4\uc758 \ud06c\uae30<\/h2>\n<p>\uad6c\uc870\uccb4\uc758 \ud06c\uae30\ub294 \uac00\uc7a5 \ud070 \ud544\ub4dc \ud0c0\uc785\uc744 \ub530\ub77c\uac04\ub2e4.<\/p>\n<p>\uac00\uc7a5 \ud070 \ud544\ub4dc \ud0c0\uc785\ubcf4\ub2e4 \ub354 \ub9ce\uc740 \ub370\uc774\ud0c0\uac00 \uc788\ub294 \uacbd\uc6b0,<br \/>\n\uad6c\uc870\uccb4\uc758 \ud06c\uae30\uac00 \ubaa8\ub4e0 \ub370\uc774\ud0c0\ub97c \ub2f4\uc744 \uc218 \uc788\uc744\ub9cc\ud07c N \ubc30 \ucee4\uc9c4\ub2e4.<\/p>\n<p>\ub450\ubc88\uc9f8 \uad6c\uc870\uccb4 <code>option_large<\/code> \ub294 <code>3 + 6 = 9<\/code> \ube44\ud2b8\uc758 \ud06c\uae30\uc774\ubbc0\ub85c,<br \/>\n<code>char(8bit) * 2<\/code> \ub9cc\ud07c \ucee4\uc9c4\ub2e4.<\/p>\n<pre><code class=\"language-c\">struct option {\n    unsigned char  flag1 : 1;\n    unsigned short flag2 : 1;\n};\n\nstruct option_large {\n    unsigned char opt1 : 3;\n    unsigned char opt2 : 6;\n};<\/code><\/pre>\n<h2>\uc800\uc7a5 \ud615\ud0dc<\/h2>\n<p>\uc2e4\uc81c \uba54\ubaa8\ub9ac\uc5d0 \uc800\uc7a5\ub418\ub294 \ubc29\uc2dd\uc740 \ub2e4\uc74c\uacfc \uac19\ub2e4.(gcc \uae30\uc900)<\/p>\n<p>\ub2e8, \uc2e4\uc81c \uc800\uc7a5\ubc29\uc2dd\uc740 \ucef4\ud30c\uc77c\ub7ec \uc885\uc18d\uc801(\ucef4\ud30c\uc77c\ub7ec \ub9d8\ub300\ub85c)\uc774\uae30 \ub54c\ubb38\uc5d0,<br \/>\n\uc2e4\uc81c\ub85c \uc5b4\ub5bb\uac8c \uc800\uc7a5\ub418\ub294\uc9c0\ub294 \ud655\uc778\ud574 \ubcf4\uc544\uc57c \ud55c\ub2e4.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.skyer9.pe.kr\/wordpress\/wp-content\/uploads\/2022\/02\/2022-02-08-02.png\" alt=\"\" \/><\/p>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\ntypedef struct _bitfield {\n    unsigned char b7 : 1;\n    unsigned char b6 : 1;\n    unsigned char b5 : 1;\n    unsigned char b4 : 1;\n    unsigned char b3 : 1;\n    unsigned char b2 : 1;\n    unsigned char b1 : 1;\n    unsigned char b0 : 1;\n} bitfield;\n\nvoid ten_to_two(unsigned char n) {\n    unsigned char a = 0x80; \/\/ 1000 0000\n    for (int i = 0; i &lt; 8; i++) {\n        if ((a &amp; n) == a)\n            printf(&quot;1&quot;);\n        else\n            printf(&quot;0&quot;);\n        a &gt;&gt;= 1;\n    }\n}\n\nint main() {\n    \/\/ ASCII : A, 10\uc9c4\uc218 : 65, 2\uc9c4\uc218 : 01000001\n    char ch = &#039;A&#039;;\n    char ch2;\n\n    printf(&quot;char : %c, 10 \uc9c4\uc218 : %d,  2\uc9c4\uc218 : &quot;, ch, ch);\n    ten_to_two(ch);\n    printf(&quot;\\n&quot;);\n\n    bitfield b;\n    b.b0 = 0;\n    b.b1 = 1;\n    b.b2 = 0;\n    b.b3 = 0;\n    b.b4 = 0;\n    b.b5 = 0;\n    b.b6 = 0;\n    b.b7 = 1;\n\n    memcpy(&amp;ch2, &amp;b, sizeof(ch2));\n    ten_to_two(ch2);\n    printf(&quot;\\n&quot;);\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>C &#8211; \uad6c\uc870\uccb4 \ube44\ud2b8 \ud544\ub4dc(struct bit field) \ucc38\uc870 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4699\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-4699","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4699","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4699"}],"version-history":[{"count":9,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4699\/revisions"}],"predecessor-version":[{"id":4710,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4699\/revisions\/4710"}],"wp:attachment":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}