Persevering with from final week’s Lesson, I found that although the C language enables you to create a Boolean array, the information is saved in char-sized integers and never true binary information. However there’s a approach to convert that Boolean space into binary information; it simply takes some coding legerdemain.
Keep in mind that in terms of the bool information kind in C, the information is saved in byte-sized (char) areas the place solely the right-most bit is counted: one or zero. Due to this fact, a bool array is conceptually ones and zeros however technically zero and one saved as character information. To drag out the binary bits and assemble a binary worth from that information, it’s essential to make use of a trick such because the one demonstrated on this code:
2026_09_12-Lesson.c
#embrace <stdio.h>
#embrace <stdbool.h>
int important()
{
bool information[] = { 0, 0, 1, 0, 0, 0, 0, 1 };
int x,d;
d = 0;
for( x=0; x<8; x++ )
= information[x];
printf("0xpercentX = %cn",d,d);
return 0;
}
The information[] array consists of eight bits of bool information. That’s the Superman half. The Clark Kent half is that information[] is mostly a char array the place solely the right-most bit in every worth is taken into account.
A for loop processes every of the bool values. Its job is to set the bits in int variable d to correspond to the binary values in bool array information[].
Variable d is asserted as an integer as storing the eight bits in a char format causes bizarre outcomes when the leftmost bit is ready. In addition to, C character features work with integers anyway. Offering that the output is interpreted as a personality, issues work out okay. Variable d is initialized to zero: d = 0
The primary assertion within the for loop bit-shifts values in variable d one place left: d <<= 1; Initially, when d is ready to zero, this assertion has no impact. However as bits are added from array information[], the impact builds the correct binary worth, as illustrated in Determine 1.
Determine 1. How bits are transferred from the bool array right into a binary worth.
The second assertion provides the bit from the right-most place for the worth information[x] to the worth saved in variable d. The result’s a correct illustration of the Boolean information in binary type. This result’s then output as each hexadecimal and character values:
0x21 = !
This strategy exhibits how one can rework a Boolean array into a real binary worth. The instance makes use of 8 bits, although you would change it to 16, 32 or no matter, offering that the code is up to date and that the ultimate storage container is of the required measurement. I suppose the true restrict is on what the binary information is representing, although it’s doable to code any answer relying on the wants.
I’ve been curious as to how C or any programming language might implement true binary storage. I might achieve this by writing a library that truly shops Boolean information in binary format. After a couple of although experiments, my conclusion is that whereas doing so is feasible the overhead would make any storage financial savings inefficient with timing and scaling. The design of recent CPUs doesn’t lend itself particularly to binary storage — even with Meeting Language. No, the strategy utilized in C for representing Boolean values might be one of the simplest ways to do it.

