43

I recently started learning to write code, and in my book I came across this question. "Why is a Boolean value stored as a byte inside of a computer when it only requires one bit?" can someone shed more light on this question?

4

3 Answers 3

53

It has to do with what the CPU can easily address. For example on an x86 processor there is an eax (32 bits), ax (16 bits) and a ah (8 bits) but no single bit register. So in order for it to use a single bit the CPU will have to do a read/modify/write to change the value. If it is stored as a byte a single read or write can be used to inspect/change the value.

Additionally one might wonder if it would be better to use a single bit vs a full byte, after all a byte will be wasting 7 bits. Unless space is a constraint the one should go for the byte because, at least the x86 and I think others, there is usually an instructions to quickly set/clear a bool which is much quicker than the read/modify/write of a single bit. From personal measurements I have seen the read/mod/write method be 5x slower than the single instruction method.

2
  • 9
    The only word you are missing is "Boundary Alignment".
    – Manoj R
    Jan 29, 2013 at 4:33
  • 3
    On the other hand, I've seen a 2x speedup from using individual bits, presumably due to better cache usage with a smaller data set. Jan 29, 2013 at 9:53
6

As @barrem23 explains, the data must be addressable, and the smallest boundary on conventional architectures is a byte.

But since this question is tagged as , it may be worth pointing out that std::vector<bool> is specialized to allow individual elements to be stored as bits. This will save space by sacrificing some functionality (for example, std::search may not work).

0

It will never be 1 bit, if you group 8 booleans in one byte, you still need 3 bits for each boolean for addressing (2^3 space), that is to know which bit inside the byte belongs to which boolean.

Also you might need one additional bit for nullability checking since you can't have a null bit, so you need one more bit for that, and you end up having 5 bits for each boolean rather than 1 byte (1 for value, 1 for null, and 3 for addressing), which is not that significant optimization.

3
  • 1
    I'm quite confused on this long sentence. These additional bits for adressing part is unclear to me. Could you give a more precise example, also related to C++ since it's one of the tags of the question? Also, in the context of this question, I don't think "nullability" has to be taken into account.
    – Pac0
    Dec 1, 2020 at 10:30
  • In normal scenario where a boolean memory reference points to the address of 1 byte in memory which contains either all zeros (for false) or all ones (for true), now to have 1 bit per boolean, you will point 8 booleans to the same byte, but you can't tell which bit inside this byte that gives you the value, so beside the normal memory reference, you will need a second reference that tells which bit inside the byte that has the value, this reference will be of size 3 bits to be able to address the 8 bits space.
    – amralieg
    Dec 2, 2020 at 3:01
  • ok, I get it now. Indeed.
    – Pac0
    Dec 2, 2020 at 20:02