Why isn't the size of a bool data type only 1 bit in C#?

12,515

Solution 1

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).

Solution 2

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

Solution 3

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! :-)

Share:
12,515

Related videos on Youtube

user1063287
Author by

user1063287

Here are some answers I hope will be helpful to others: JavaScript If primitives are immutable and have no methods, why can a method be called on a string? Security How to secure a comment form in a non-CMS environment with no user authentication? Node How to use Helmet to define Content Security Policy? How do routes, middleware and next work in Express? How to split a string into chunks of a particular byte size? OpenShift How to view OpenShift Online node application logs locally? How to connect to OpenShift 3 MongoDB remotely? Miscellaneous (Git Bash) How to create a multi-terminal Git Bash environment with Windows Terminal? (Codepen) How to add a syntax highlighting theme to codepen? And here are some links to recommended learning content: JavaScript: Understanding the Weird Parts - The First 3.5 Hours (YouTube video link) This is a great, and deep, introduction to JavaScript that will answer a lot of questions new JS developers will have about common terminology and dynamics. I wish I had viewed it when I first started.

Updated on June 06, 2022

Comments

  • user1063287
    user1063287 about 2 years

    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.

    • Sayse
      Sayse almost 11 years
      This may help you
    • Ian Kemp
      Ian Kemp almost 7 years
    • user1063287
      user1063287 almost 7 years
      This question was asked 4 years ago and has some valuable answers, it would be a pity if it was removed as a duplicate.
  • Tyler
    Tyler almost 11 years
    Where are you finding the size of a Boolean variable to be 4 bytes?
  • Tim S.
    Tim S. almost 11 years
  • Tyler
    Tyler almost 11 years
    You should say a max of 4 bytes then because it may not actually end up taking up 4 bytes.
  • Luaan
    Luaan about 9 years
    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.
  • James
    James over 5 years
    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.