Why does a boolean require one byte instead of one bit?

liptonIcedTea

Well-known member
Joined
Jan 18, 2007
Messages
89
Programming Experience
1-3
Probably not an important question, but just out of curiousity...

Why, in .NET, does a System.Boolean value type requires one byte?

I mean, shouldn't it only require 1 bit to tell that it is either true or false? What does it do for the other 7 bits?
 
but the question is why does it need to allocate a full byte? is there some technical reason why types can not be smaller than 1 byte?
 
but the question is why does it need to allocate a full byte? is there some technical reason why types can not be smaller than 1 byte?
As I said, the full byte is allocated. Computer memory is measured in bytes, so a single bit isn't going to be allocated for the use of a boolean value. If you think of bits as eggs and bytes as egg cartons, you can't buy just one egg. If you want an egg then you have to buy a carton. Whether you choose to use the other eggs or not is up to you but you're getting them regardless.
 
A little rusty on this, but at higher level I think tis kept simple by using "chunks".

Byte, Word, Long Word

8 bits, 16 bits, 32 bits..


I see your logic in saying that you could in theory have 8 booleans defined in 1 byte.

Im not 100% on how it works at that low level, but obviously if it uses 11111111 for true and 00000000 for false that represents FF and 00 in Hex.

I'm guessing it's also related to the way memory is addressed..
For example when you " Dim A,B as boolean" it may work in the following way :-

A = address location 1000
B = address location 1001

each address location representing 1 byte of memory.


If you started using bits it would need to know which declared booleans were pointing to which bit.. so if you defined 3 booleans, at address 1000, and address 1000 returned "3" , how does it know which boolean = 0?


I'm not sure if I have explained it very well as im a bit rusty and guessing some of it.


I guess if you really wanted to squeeze 8 booleans into 1 byte , you could define a byte and then use the integer up to 255 to represent the "bits" ?


Hopefully I didn't just waffle on aload of rubbish! :D
 
Back
Top