{"id":56,"date":"2009-03-10T22:29:26","date_gmt":"2009-03-10T20:29:26","guid":{"rendered":"http:\/\/www.cetinkoca.com\/blog\/?p=56"},"modified":"2014-12-29T00:20:22","modified_gmt":"2014-12-28T22:20:22","slug":"bit-fields","status":"publish","type":"post","link":"https:\/\/www.cetinkoca.com\/blog\/bit-fields\/","title":{"rendered":"Bit Fields"},"content":{"rendered":"<p>Bit fields are probably one of the least popular language features of C\/C++. By using bit fields the programmer can precisely define how many bits are occupied by each member of a <code>struct<\/code> or <code>class<\/code>.<!--more--> The syntax for bit fields follows:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nstruct Time\r\n{\r\n\tunsigned int pm : 1;\r\n\tunsigned int hour : 4;\r\n\tunsigned int minute : 6;\r\n};\r\n<\/pre>\n<p>The member field called <code>pm<\/code> of the <code>struct Time<\/code> above occupies only 1-bit, <code>hour<\/code> occupies 4-bits and <code>minute<\/code> occupies 6-bits.<\/p>\n<p>Note that the members specified as a bit-field are always treated as integers, so you can omit the keyword <code>int<\/code> from the definition as follows:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nstruct Time\r\n{\r\n\tunsigned pm : 1;\r\n\tunsigned hour : 4;\r\n\tunsigned minute : 6;\r\n};\r\n<\/pre>\n<p>Bit fields are accessed just as any other member:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nint main()\r\n{\r\n\tTime time;\r\n\ttime.pm = true;\r\n\ttime.hour = 10;\r\n\ttime.minute = 22;\r\n\r\n\tprintf(&quot;The time is %d:%d %sn&quot;,\r\n\t\ttime.hour, time.minute, time.pm ? &quot;PM&quot; : &quot;AM&quot;);\r\n\r\n\treturn 0;\r\n}\r\n<\/pre>\n<p>Bit fields are actually useful for:<\/p>\n<ul>\n<li>Packing several objects into one or more bytes, hence precise control over memory occupation in terms of bits<\/li>\n<li>Creating structs suitable for reading data from, ans possibly writing data to, external file formats as well as transmitting data over network connections<\/li>\n<\/ul>\n<p>Although it is unlikely that you would need to use bit fields in your program, if you ever do so make sure you pay attention to the following:<\/p>\n<ul>\n<li>Portability issues<\/li>\n<li>Thread-synchronization issues<\/li>\n<\/ul>\n<p>I will not go into the details here. If you would like to know more about bit fields check following links:<\/p>\n<p><strong>msdn:<\/strong>??<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/yszfawxh.aspx\" target=\"_blank\">C Bit Fields<\/a><br \/>\n<strong>msdn:<\/strong> <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ewwyfdbe.aspx\" target=\"_blank\">C++ Bit Fields<\/a><br \/>\n<strong>Wikipedia:<\/strong> <a href=\"http:\/\/en.wikipedia.org\/wiki\/Bit_field\" target=\"_blank\">Bit field<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bit fields are probably one of the least popular language features of C\/C++. By using bit fields the programmer can precisely define how many bits are occupied by each member of a struct or class.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[7],"tags":[10,15],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5z32a-U","_links":{"self":[{"href":"https:\/\/www.cetinkoca.com\/blog\/wp-json\/wp\/v2\/posts\/56"}],"collection":[{"href":"https:\/\/www.cetinkoca.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cetinkoca.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cetinkoca.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cetinkoca.com\/blog\/wp-json\/wp\/v2\/comments?post=56"}],"version-history":[{"count":5,"href":"https:\/\/www.cetinkoca.com\/blog\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"predecessor-version":[{"id":125,"href":"https:\/\/www.cetinkoca.com\/blog\/wp-json\/wp\/v2\/posts\/56\/revisions\/125"}],"wp:attachment":[{"href":"https:\/\/www.cetinkoca.com\/blog\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cetinkoca.com\/blog\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cetinkoca.com\/blog\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}