Blogus Maximus

Rubbing people the wrong way since 1970...

  Home  |   Contact  |   Syndication    |   Login
  1366 Posts | 10 Stories | 2228 Comments | 1336 Trackbacks


Google My Blog

Catch me at: The List!

My InstallScript Utility Belt My Amazon Wishlist
My Standard Disclaimer

Tag Cloud

Article Categories



Code Camps

CTown Geeks

Geeky Webcomics

High Geek

Magenic Blogs

Microsoft Blogs

My Articles

My Sites




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


# re: can anyone tell me why? 9/18/2005 4:02 PM Geoff Appleby
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.

# re: can anyone tell me why? 9/18/2005 6:48 PM Michael Van Cleave
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.

# re: can anyone tell me why? 9/19/2005 3:25 AM Chris Williams
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.

# re: can anyone tell me why? 9/19/2005 7:44 AM Paul Vick
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...

Post A Comment