{"id":168,"date":"2014-08-02T01:29:53","date_gmt":"2014-08-02T07:29:53","guid":{"rendered":"http:\/\/jacobncalvert.com\/?p=168"},"modified":"2018-02-10T15:55:45","modified_gmt":"2018-02-10T21:55:45","slug":"binary-bit-fields-and-flags","status":"publish","type":"post","link":"https:\/\/jacobncalvert.com\/blog-archive\/2014\/08\/02\/binary-bit-fields-and-flags\/","title":{"rendered":"Binary bit fields and flags"},"content":{"rendered":"<p>If you&#8217;ve used any legacy C libraries before, you&#8217;ve probably used these things called bit fields, even if you didn&#8217;t know what they were.<\/p>\n<p>Bit fields are a way to efficiently store multiple boolean values in one or more bytes. If you can imagine an 8 bit integer as binary, each bit would have to be either a 1 or a 0, corresponding to an &#8220;On&#8221; or &#8220;Off&#8221; value. With just one 8 bit variable, you can store 8 on\/off values for your program. The syntax on how to set, unset, and retrieve these values can be a little confusing however. The method for using these helpful little bit fields involves some binary math with the binary operators AND, OR, and NOT. In C based languages these are written as &amp;, |, and, ~, respectively. I&#8217;ll give an example in C that will hopefully make this math clearer. First we need to define some values as our flags. Each flag has to have a value that is a power of 2. For example:<\/p>\n<pre class=\"prettyprint\">enum FLAGS\r\n{\r\n\tFLAG_A = 0x01,\/\/ 1\r\n\tFLAG_B = 0X02,\/\/ 2\r\n\tFLAG_C = 0X04,\/\/ 4\r\n\tFLAG_D = 0X08,\/\/ 8\r\n\tFLAG_E = 0X10,\/\/ 16\r\n\tFLAG_F = 0X20 \/\/ 32\r\n\r\n\t\/\/ so on and so forth\r\n};\r\n<\/pre>\n<p>These could just as easily be written as the actual integer values, however I&#8217;ll stick to HEX notation for this example. Next we need a variable of at least 6 bits to hold our flags&#8217; on or off state. Let&#8217;s use an integer type, and set all the flags to &#8220;Off.&#8221;<\/p>\n<pre class=\"prettyprint\">static int THE_OPTION = 0x00;\r\n<\/pre>\n<p>Now we&#8217;re ready to set, unset, and test some flags! To set the option to &#8220;On&#8221; for a certain flag, we must turn the bit &#8220;On&#8221; in our variable. To do this we use the OR operation. I explain why in the example below.<\/p>\n<pre class=\"prettyprint\">FLAG_C = 4, in 8-bit binary this is 00000100\r\nTHE_OPTION = 0, in 8-bit binary\t    00000000\r\n\r\nif we OR them:\r\n\r\nTHE_OPTION |= FLAG_C\r\n\r\nthe binary math looks like:\r\n\r\n\t00000000\r\nOR\t00000100\r\n-------------\r\n\t00000100\r\n\r\n<\/pre>\n<p>This is a simple example but the point holds. To apply a flag, we apply the OR operator. To remove an option, we use the AND operator.<\/p>\n<pre class=\"prettyprint\">THE_OPTION = 0B00000100; \/\/binary notation for 4\r\n\r\nTHE_OPTION &amp;= ~FLAG_C; \/\/ THE_OPTION AND-EQUALS NOT FLAG_C\r\n\r\nthe binary math looks like:\r\n\r\n\t00000100\r\nAND\t11111011\r\n-------------\r\n\t00000000\r\n\r\n<\/pre>\n<p>To check the status of a flag in THE_OPTION, one can simply check:<\/p>\n<pre class=\"prettyprint\">if(THE_OPTION &amp; FLAG_F)\r\n{\r\n\t\/\/do things\r\n}\r\n<\/pre>\n<p>These can also be chained, for instance<\/p>\n<pre class=\"prettyprint\">#include \"stdio.h\"\r\nenum FLAGS\r\n{\r\n\tFLAG_A = 0x01,\/\/ 1\r\n\tFLAG_B = 0X02,\/\/ 2\r\n\tFLAG_C = 0X04,\/\/ 4\r\n\tFLAG_D = 0X08,\/\/ 8\r\n\tFLAG_E = 0X10,\/\/ 16\r\n\tFLAG_F = 0X20 \/\/ 32\r\n\r\n\t\/\/ so on and so forth\r\n};\r\n\r\nstatic int THE_OPTION = 0x00;\r\nvoid test_options()\r\n{\r\n\tchar line[512], *p;\r\n\tp = &amp;line;\r\n\tp += sprintf(p,\"THE_OPTION contains \");\r\n\tif(FLAG_A &amp; THE_OPTION)\r\n\t{\r\n\t\tp += sprintf(p, \"FLAG_A and \");\r\n\t}\r\n\tif(FLAG_B &amp; THE_OPTION)\r\n\t{\r\n\t\tp += sprintf(p, \"FLAG_B and \");\r\n\t}\r\n\tif(FLAG_C &amp; THE_OPTION)\r\n\t{\r\n\t\tp += sprintf(p, \"FLAG_C and \");\r\n\t}\r\n\tif(FLAG_D &amp; THE_OPTION)\r\n\t{\r\n\t\tp += sprintf(p, \"FLAG_D and \");\r\n\t}\r\n\tif(FLAG_E &amp; THE_OPTION)\r\n\t{\r\n\t\tp += sprintf(p, \"FLAG_E and \");\r\n\t}\r\n\tif(FLAG_F &amp; THE_OPTION)\r\n\t{\r\n\t\tp += sprintf(p, \"FLAG_F and \");\r\n\t}\r\n\tprintf(\"%s, that is all.\\n\", line);\r\n}\r\nint main()\r\n{\r\n\r\n\tTHE_OPTION = (FLAG_A | FLAG_C | FLAG_F);\r\n\ttest_options();\r\n\r\n\tTHE_OPTION &amp;= ~FLAG_A;\r\n\ttest_options();\r\n\r\n\tTHE_OPTION &amp;= ~(FLAG_C | FLAG_F);\r\n\ttest_options();\r\n\r\n\tTHE_OPTION = 0XFF;\r\n\ttest_options();\r\n\treturn 0;\r\n}\r\n\r\n\r\n\r\n<\/pre>\n<p>That will print<\/p>\n<pre class=\"prettyprint\">THE_OPTION contains FLAG_A and FLAG_C and FLAG_F and , that is all.\r\nTHE_OPTION contains FLAG_C and FLAG_F and , that is all.\r\nTHE_OPTION contains , that is all.\r\nTHE_OPTION contains FLAG_A and FLAG_B and FLAG_C and FLAG_D and FLAG_E and FLAG_F and , that is all.\r\n<\/pre>\n<p>This post is by no means an exhaustive list of the ways bit fields can be used, however these are the basic ways you can use them. Bit fields are a good choice in applications where memory is limited, or there are lots of boolean data points that need to be stored since one 32 bit variable can hold all the same data as 32 bool types in C. I hope this post was clear and somewhat educational! If you have anything to add send me some mail over on the\u00a0<a href=\"http:\/\/jacobncalvert.com\/contact\/\">contact page!<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;ve used any legacy C libraries before, you&#8217;ve probably used these things called bit fields, even if you didn&#8217;t know what they were. Bit fields are a way to efficiently store multiple boolean values in one or more bytes. If you can imagine an 8 bit integer as binary, each bit would have to be either a 1 or a 0, corresponding to an &#8220;On&#8221; or &#8220;Off&#8221; value. With just one 8 bit variable, you can store 8 on\/off&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[72],"tags":[11],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Binary bit fields and flags - Jacob N Calvert<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jacobncalvert.com\/blog-archive\/2014\/08\/02\/binary-bit-fields-and-flags\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Binary bit fields and flags - Jacob N Calvert\" \/>\n<meta property=\"og:description\" content=\"If you&#8217;ve used any legacy C libraries before, you&#8217;ve probably used these things called bit fields, even if you didn&#8217;t know what they were. Bit fields are a way to efficiently store multiple boolean values in one or more bytes. If you can imagine an 8 bit integer as binary, each bit would have to be either a 1 or a 0, corresponding to an &#8220;On&#8221; or &#8220;Off&#8221; value. With just one 8 bit variable, you can store 8 on\/off&hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jacobncalvert.com\/blog-archive\/2014\/08\/02\/binary-bit-fields-and-flags\/\" \/>\n<meta property=\"og:site_name\" content=\"Jacob N Calvert\" \/>\n<meta property=\"article:published_time\" content=\"2014-08-02T07:29:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-02-10T21:55:45+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jacobncalvert.com\/blog-archive\/#website\",\"url\":\"https:\/\/jacobncalvert.com\/blog-archive\/\",\"name\":\"Jacob N Calvert\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/jacobncalvert.com\/blog-archive\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jacobncalvert.com\/blog-archive\/2014\/08\/02\/binary-bit-fields-and-flags\/#webpage\",\"url\":\"https:\/\/jacobncalvert.com\/blog-archive\/2014\/08\/02\/binary-bit-fields-and-flags\/\",\"name\":\"Binary bit fields and flags - Jacob N Calvert\",\"isPartOf\":{\"@id\":\"https:\/\/jacobncalvert.com\/blog-archive\/#website\"},\"datePublished\":\"2014-08-02T07:29:53+00:00\",\"dateModified\":\"2018-02-10T21:55:45+00:00\",\"author\":{\"@id\":\"https:\/\/jacobncalvert.com\/blog-archive\/#\/schema\/person\/f4b22a996d41bf09ed2bbe22912a8c8a\"},\"breadcrumb\":{\"@id\":\"https:\/\/jacobncalvert.com\/blog-archive\/2014\/08\/02\/binary-bit-fields-and-flags\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jacobncalvert.com\/blog-archive\/2014\/08\/02\/binary-bit-fields-and-flags\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jacobncalvert.com\/blog-archive\/2014\/08\/02\/binary-bit-fields-and-flags\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jacobncalvert.com\/blog-archive\/\",\"url\":\"https:\/\/jacobncalvert.com\/blog-archive\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@id\":\"https:\/\/jacobncalvert.com\/blog-archive\/2014\/08\/02\/binary-bit-fields-and-flags\/#webpage\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/jacobncalvert.com\/blog-archive\/#\/schema\/person\/f4b22a996d41bf09ed2bbe22912a8c8a\",\"name\":\"Jacob\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/jacobncalvert.com\/blog-archive\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/64a2dd1c00cb39dfc19bb1204c87efbc?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/64a2dd1c00cb39dfc19bb1204c87efbc?s=96&d=mm&r=g\",\"caption\":\"Jacob\"},\"sameAs\":[\"https:\/\/jacobncalvert.com\"],\"url\":\"https:\/\/jacobncalvert.com\/blog-archive\/author\/jcalvert\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/jacobncalvert.com\/blog-archive\/wp-json\/wp\/v2\/posts\/168"}],"collection":[{"href":"https:\/\/jacobncalvert.com\/blog-archive\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jacobncalvert.com\/blog-archive\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jacobncalvert.com\/blog-archive\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jacobncalvert.com\/blog-archive\/wp-json\/wp\/v2\/comments?post=168"}],"version-history":[{"count":1,"href":"https:\/\/jacobncalvert.com\/blog-archive\/wp-json\/wp\/v2\/posts\/168\/revisions"}],"predecessor-version":[{"id":169,"href":"https:\/\/jacobncalvert.com\/blog-archive\/wp-json\/wp\/v2\/posts\/168\/revisions\/169"}],"wp:attachment":[{"href":"https:\/\/jacobncalvert.com\/blog-archive\/wp-json\/wp\/v2\/media?parent=168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jacobncalvert.com\/blog-archive\/wp-json\/wp\/v2\/categories?post=168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jacobncalvert.com\/blog-archive\/wp-json\/wp\/v2\/tags?post=168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}