Building a Boolean Array

Continuing from last week’s Lesson, I discovered that though the C language lets you create a Boolean array, the data is stored in char-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.

Remember that when it comes to the bool data type in C, the data is stored in byte-sized (char) locations where only the right-most bit is counted: one or zero. Therefore, a bool 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:

2026_09_12-Lesson.c

#include <stdio.h>
#include <stdbool.h>

int main()
{
    bool data[] = { 0, 0, 1, 0, 0, 0, 0, 1 };
    int x,d;

    /* build binary character */
    d = 0;
    for( x=0; x<8; x++ )
    {
        d <<= 1;
        d |= data[x];
    }

    printf("0x%X = %c\n",d,d);

    return 0;
}

The data[] array consists of eight bits of bool data. That’s the Superman part. The Clark Kent part is that data[] is really a char array where only the right-most bit in each value is considered.

A for loop processes each of the bool values. Its job is to set the bits in int variable d to correspond to the binary values in bool array data[].

Variable d is declared as an integer as storing the eight bits in a char 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 d is initialized to zero: d = 0

The first statement in the for loop bit-shifts values in variable d one position left: d <<= 1; Initially, when d is set to zero, this statement has no effect. But as bits are added from array data[], the effect builds the proper binary value, as illustrated in Figure 1.

Setting bits animation

Figure 1. How bits are transferred from the bool array into a binary value.

The second statement adds the bit from the right-most position for the value data[x] to the value stored in variable d. The result is a proper representation of the Boolean data in binary form. This result is then output as both hexadecimal and character values:

0x21 = !

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’s possible to code any solution depending on the needs.

I’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’t lend itself specifically to binary storage — even with Assembly Language. No, the approach used in C for representing Boolean values is probably the best way to do it.

Leave a Reply