One-byte bool. Why?

Why does a bool require one byte to store true or false where just one bit is enough

Because every object in C++ must be individually addressable* (that is, you must be able to have a pointer to it). You cannot address an individual bit (at least not on conventional hardware).

How much safer is it to use the following?

It’s “safe”, but it doesn’t achieve much.

is the above field technique really going to help?

No, for the same reasons as above 😉

but still compiler generated code to access them is bigger and slower than the code generated to access the primitives.

Yes, this is true. On most platforms, this requires accessing the containing byte (or int or whatever), and then performing bit-shifts and bit-mask operations to access the relevant bit.


If you’re really concerned about memory usage, you can use a std::bitset in C++ or a BitSet in Java, which pack bits.


* With a few exceptions.

Leave a Comment