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?
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
It has to do with what the CPU can easily address. For example on an x86 processor there is an
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.
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 c++, it may be worth pointing out that