29

I am just learning C# and looking deeper into data types.

Why isn't a bool data type 1 bit in size?

It seems it can only hold one of two values (true or false), so wouldn't that only take up 1 bit of space to represent that value?

Is it because the smallest 'addressable' size of a value is a byte (8 bits) as referred to in this post?

My overall aim was to logically envisage the different size of each data type in C# so I was trying to create a list of all data types and their allocated bit size and this threw me.

3
  • This may help you Commented Jul 19, 2013 at 11:57
  • Possible duplicate of Why in .NET System.Boolean takes 4 byte? Commented Jul 21, 2017 at 13:50
  • 1
    This question was asked 4 years ago and has some valuable answers, it would be a pity if it was removed as a duplicate. Commented Jul 21, 2017 at 19:22

3 Answers 3

26

Is it because the smallest 'addressable' size of a value is a byte

Yep, exactly the same thing. In order for the CLR to be efficient, it maps its data types to the native machine data types in much the same way as the compiler does in C++ (pretty much).

Sign up to request clarification or add additional context in comments.

2 Comments

And it doesn't do any packing, ever - so using 8 bools could be represented as a single byte; but it never is. Pascal is an example of a language that does handle that for you, but .NET certainly doesn't do that.
Ironically, that choice makes it super inefficient, not efficient: Most computations are memory bound these days: a cache miss takes as long as up to hundreds of register operations. Multiplying memory use by a factor of 8 in order to save a shift and a mask for each index operation wasn't even a good idea in the 1980's, much less so now.
4

If you want to store lots of flags in a space-efficient way, consider using Int32 or Int64 as a bitmask, this way you can store 32 or 64 boolean flags in a 32 / 64 bit data type. You have to do bitmask tests to check or set values, so there's a small addition cost to access or update, over a Boolean variable.

The size of a Boolean field in memory is 1 byte, and of a Boolean variable is 4 bytes.

BitArray is also handy for dealing with lots of bit flags: http://msdn.microsoft.com/en-us/library/system.collections.bitarray.aspx

3 Comments

Where are you finding the size of a Boolean variable to be 4 bytes?
You should say a max of 4 bytes then because it may not actually end up taking up 4 bytes.
2

I noticed this as well... I created two arrays: float[4000] and float?[4000]. The second array takes twice the memory space because float? is implemented as a float and a bool, and the bool ends up taking 32 bits just the same as the float does.

So in the end, if memory usage is a concern, using a NaN float value to represent "null" in a float[] is better than using a float?[].

Makes me feel like an idiot for all the years I tried to use smaller data types believing it was actually doing some good! :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.