[try Beta version]
Not logged in

 
sizeof a boolean variable ???

Dec 23, 2008 at 4:02pm
what is the sizeof boolean variable

does it actually use only one bit

if not why not???

can anybody explain
Dec 23, 2008 at 4:09pm
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.
Dec 23, 2008 at 4:17pm
sizeof(bool) returns 1

sizeof(char) returns 1 as well

perhaps u shud look into it LacViet


Dec 23, 2008 at 4:18pm
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 Dec 23, 2008 at 4:18pm
Dec 23, 2008 at 5:54pm
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.
Dec 23, 2008 at 6:16pm
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 Dec 23, 2008 at 6:17pm
Dec 23, 2008 at 7:25pm
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 Dec 23, 2008 at 7:26pm
Dec 23, 2008 at 7:27pm
Topic archived. No new replies allowed.