sizeof a boolean variable ???

what is the sizeof boolean variable

does it actually use only one bit

if not why not???

can anybody explain
You got it. Just one bit. Basically you only need it to hold 2 values, 0 for false, true for anything else. You can use the sizeof() operator anytime to find the size of your data variable.
sizeof(bool) returns 1

sizeof(char) returns 1 as well

perhaps u shud look into it LacViet


The minimum size for a variable is 1 byte, 1 bit booleans could be created in a bit filed
sizeof() returns the size in bytes
Last edited on
The reason why the minimum size of a variable is 1 byte it has to do with the way computers access memory. There is no way of reserving just a single bit of memory for a variable. You might say memory is accessed with a "resolution" of eight bits (or one byte).

I believe even the one bit boolean created using a bit field as mentioned by Bazzy would require a complete byte to store.
A bit field is an std::vector<bool>. It is a specialization of vector designed to optimize memory utilization. Every element in a bit field uses exactly 1 bit. The capacity of a bit field can only grow in multiples of 8, though.
Last edited on
helios, I believe you're thinking of std::bitset, not regular old bit fields. C++ also supports actual bit fields.

Here is some msdn documentation on bit fields (http://msdn.microsoft.com/en-us/library/ewwyfdbe.aspx).

But just to emphasize that it's not a microsoft extension: http://www.research.att.com/~bs/glossary.html#Gbitfield

The "TC++PL C.8.1" refers you to "The C++ Programming Language" book by Stroustrup.
Last edited on
Topic archived. No new replies allowed.