{"id":32,"date":"2008-06-04T23:05:00","date_gmt":"2008-06-05T06:05:00","guid":{"rendered":"http:\/\/samueldotj.com\/blog\/?p=32"},"modified":"2013-08-26T14:42:28","modified_gmt":"2013-08-26T21:42:28","slug":"bitfield-inside-a-union","status":"publish","type":"post","link":"http:\/\/samueldotj.com\/blog\/bitfield-inside-a-union\/","title":{"rendered":"Bitfield inside a Union"},"content":{"rendered":"<p>When coding Ace physical memory manager, I came to a situation where I have to use lot of bit operations on a integer variable. The code looked little ugly because of the explicit bit operations(left shift<br \/>\n[c]<br \/>\ntypedef struct struct_x<br \/>\n{<br \/>\n union<br \/>\n {<br \/>\n  int all;<br \/>\n  int x:1,y:1,z:30,<br \/>\n };<br \/>\n};<br \/>\n[\/c]<br \/>\nThe above code wasn\u2019t working; debugging further I found that the values for bitfields are not calculated correctly.<\/p>\n<p>After some digging I found that, the bitfield hint is ignored by the compiler since it is directly under union. So the compiler treated the fields x,y,z as separate integer instead of considering as one.<\/p>\n<p>I modified the code to pack the bitfields inside a structure and it worked correctly.<br \/>\n[c]<br \/>\ntypedef struct struct_x<br \/>\n{<br \/>\n union<br \/>\n {<br \/>\n  int all;<br \/>\n  struct<br \/>\n  {<br \/>\n   int x:1, y:1, z:30,<br \/>\n  }_;<br \/>\n };<br \/>\n};<br \/>\n[\/c]<br \/>\nAnd yes, just an underscore(_) is a valid identifier.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When coding Ace physical memory manager, I came to a situation where I have to use lot of bit operations on a integer variable. The code looked little ugly because of the explicit bit operations(left shift [c] typedef struct struct_x { union { int all; int x:1,y:1,z:30, }; }; [\/c] The above code wasn\u2019t working; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,5],"tags":[],"class_list":["post-32","post","type-post","status-publish","format-standard","hentry","category-c","category-programming"],"_links":{"self":[{"href":"http:\/\/samueldotj.com\/blog\/wp-json\/wp\/v2\/posts\/32","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/samueldotj.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/samueldotj.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/samueldotj.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/samueldotj.com\/blog\/wp-json\/wp\/v2\/comments?post=32"}],"version-history":[{"count":2,"href":"http:\/\/samueldotj.com\/blog\/wp-json\/wp\/v2\/posts\/32\/revisions"}],"predecessor-version":[{"id":119,"href":"http:\/\/samueldotj.com\/blog\/wp-json\/wp\/v2\/posts\/32\/revisions\/119"}],"wp:attachment":[{"href":"http:\/\/samueldotj.com\/blog\/wp-json\/wp\/v2\/media?parent=32"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/samueldotj.com\/blog\/wp-json\/wp\/v2\/categories?post=32"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/samueldotj.com\/blog\/wp-json\/wp\/v2\/tags?post=32"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}