Geeks With Blogs


Google My Blog

Catch me at: The List!

My InstallScript Utility Belt My Amazon Wishlist
My Standard Disclaimer

Chris G. Williams Beware: I mix tech and personal interests here.
Can anyone tell me why a Boolean reserves 2 Bytes of memory, as opposed to 1 bit (0 or 1) or some other amount (smaller than 2 Bytes)? Thanks! Posted on Sunday, September 18, 2005 2:49 PM | Back to top

Comments on this post: can anyone tell me why?

# re: can anyone tell me why?
Requesting Gravatar...
How did you measure it to be 2 bytes?

According to the CLI standard, a boolean is only supposed to take up 1 byte of memory. However, the VB Boolean, which maps to System.Boolean, is a struct that contains private members and stuff like that, so maybe while an actual bool integral type in IL is one byte, the system.boolean is larger with all it's other stuff inside it.
Left by Geoff Appleby on Sep 18, 2005 4:02 PM

# re: can anyone tell me why?
Requesting Gravatar...
I would have to agree with Geoff. Although the boolean takes up just a bit, the other parts of the structure that add all of the functionality that make our lives easy.

BTW, I had a student of mine ask this very same question just this last week.
Left by Michael Van Cleave on Sep 18, 2005 6:48 PM

# re: can anyone tell me why?
Requesting Gravatar...
The Microsoft Course 2559 (Intro to VB.NET) contains a graphic that shows the memory allocation for the various data types. It lists boolean as 2 Bytes and I wasn't sure why. Apparently it’s more efficient for the computations since the small registers hold 2 bytes anyways. Otherwise you’d get into partially filled registers, and it’s just easier to square off the value.
Left by Chris Williams on Sep 19, 2005 3:25 AM

# re: can anyone tell me why?
Requesting Gravatar...
Actually, a Boolean is 4 bytes, not 2. The reason is that that's what the CLR supports for Boolean. I think that's what it does because 32 bit values are much more efficient to manipulate, so the time/space tradeoff is, in general, worth it. You should use the bit vector class (forget where it is) if you need to jam a bunch of bits together...
Left by Paul Vick on Sep 19, 2005 7:44 AM

Your comment:
 (will show your gravatar)

Copyright © Chris G. Williams | Powered by: