{"id":7764,"date":"2026-09-12T00:01:54","date_gmt":"2026-09-12T07:01:54","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7764"},"modified":"2026-09-05T12:19:36","modified_gmt":"2026-09-05T19:19:36","slug":"building-a-boolean-array","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7764","title":{"rendered":"Building a Boolean Array"},"content":{"rendered":"<p>Continuing from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7752\">last week&#8217;s Lesson<\/a>, I discovered that though the C language lets you create a Boolean array, the data is stored in <em>char<\/em>-sized integers and not true binary data. But there is a way to convert that Boolean area into binary data; it just takes some coding legerdemain.<br \/>\n<!--more--><br \/>\nRemember that when it comes to the <em>bool<\/em> data type in C, the data is stored in byte-sized (<em>char<\/em>) locations where only the right-most bit is counted: one or zero. Therefore, a <em>bool<\/em> array is conceptually ones and zeros but technically zero and one stored as character data. To pull out the binary bits and construct a binary value from that data, you must employ a trick such as the one demonstrated in this code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_09_12-Lesson.c\" rel=\"noopener\" target=\"_blank\">2026_09_12-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdbool.h&gt;\r\n\r\nint main()\r\n{\r\n    bool data[] = { 0, 0, 1, 0, 0, 0, 0, 1 };\r\n    int x,d;\r\n\r\n    <span class=\"comments\">\/* build binary character *\/<\/span>\r\n    d = 0;\r\n    for( x=0; x&lt;8; x++ )\r\n    {\r\n        d &lt;&lt;= 1;\r\n        d |= data[x];\r\n    }\r\n\r\n    printf(\"0x%X = %c\\n\",d,d);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The <code>data[]<\/code> array consists of eight bits of <em>bool<\/em> data. That&#8217;s the Superman part. The Clark Kent part is that <code>data[]<\/code> is really a <em>char<\/em> array where only the right-most bit in each value is considered.<\/p>\n<p>A <em>for<\/em> loop processes each of the <em>bool<\/em> values. Its job is to set the bits in <em>int<\/em> variable <code>d<\/code> to correspond to the binary values in <em>bool<\/em> array <code>data[]<\/code>.<\/p>\n<p>Variable <code>d<\/code> is declared as an integer as storing the eight bits in a <em>char<\/em> format causes weird results when the leftmost bit is set. Besides, C character functions work with integers anyway. Providing that the output is interpreted as a character, things work out okay. Variable <code>d<\/code> is initialized to zero: <code>d = 0<\/code><\/p>\n<p>The first statement in the <em>for<\/em> loop bit-shifts values in variable <code>d<\/code> one position left: <code>d &lt;&lt;= 1;<\/code> Initially, when <code>d<\/code> is set to zero, this statement has no effect. But as bits are added from array <code>data[]<\/code>, the effect  builds the proper binary value, as illustrated in Figure 1.<\/p>\n<div id=\"attachment_7768\" style=\"width: 509px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7768\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/09\/0912-Figure1.gif\" alt=\"Setting bits animation\" width=\"499\" height=\"286\" class=\"size-full wp-image-7768\" \/><p id=\"caption-attachment-7768\" class=\"wp-caption-text\">Figure 1. How bits are transferred from the <em>bool<\/em> array into a binary value.<\/p><\/div>\n<p>The second statement adds the bit from the right-most position for the value <code>data[x]<\/code> to the value stored in variable <code>d<\/code>. The result is a proper representation of the Boolean data in binary form. This result is then output as both hexadecimal and character values:<\/p>\n<pre>0x21 = !<\/pre>\n<p>This approach shows how you can transform a Boolean array into a true binary value. The example uses 8 bits, though you could change it to 16, 32 or whatever, providing that the code is updated and that the final storage container is of the required size. I suppose the true limit is on what the binary data is representing, though it&#8217;s possible to code any solution depending on the needs.<\/p>\n<p>I&#8217;ve been curious as to how C or any programming language could implement true binary storage. I could do so by writing a library that actually stores Boolean data in binary format. After a few though experiments, my conclusion is that while doing so is possible the overhead would make any storage savings inefficient with timing and scaling. The design of modern CPUs doesn&#8217;t lend itself specifically to binary storage &mdash; even with Assembly Language. No, the approach used in C for representing Boolean values is probably the best way to do it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Though the array is Boolean, the data is not. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7764\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-7764","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7764"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7764\/revisions"}],"predecessor-version":[{"id":7774,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7764\/revisions\/7774"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}