Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
This isn't strictly speaking true. Some languages (like C/C++) provide ways of storing boolean values as single bits, if these bits are bundled in a single byte. In other words, in C, you can store eight booleans in a byte. But few ever use this since memory is cheap and processors manipulate data in 8/16/32/64 bit chunks. – Steven Burnap Jan 29 '13 at 2:51
This answer to a similar question is what you should see! You'll get huge reference there... Cheers! – Sam Jan 29 '13 at 5:09
Closely related:… – Ben Lee Feb 4 '13 at 22:24

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.

share|improve this answer
The only word you are missing is "Boundary Alignment". – Manoj R Jan 29 '13 at 4:33
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. – Michael Borgwardt Jan 29 '13 at 9:53

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).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.