5

Possible Duplicate:
What is the binary representation of a boolean value in c#

According to the MSDN documentation, the sizeof keyword is "used to obtain the size in bytes for an unmanaged type" and primitives are considered unmanaged types. If I check the sizeof(bool), the result is 1.

It seems to me that using a Boolean value should only require a bit of memory. Am I mistaken? Does using a Boolean value actually requires a full byte of memory? Why?

Community
  • 1
  • 1
smartcaveman
  • 41,281
  • 29
  • 127
  • 212

4 Answers4

14

It uses a whole byte of memory for performance reasons.

If it only used a single bit, what do you do with the other 7 bits? Few variables are booleans, and other variables may not require a single bit. So it would only be useful for other booleans.

For example, 4-byte integers. Also, many larger types need to start at appropriate byte boundaries for performance reasons. For example, a CPU may not allow you to easily reference a 4-byte address starting from any address (ie. the address may need to be divisible by 4).

If it used a single bit of memory, meaning the other 7-bits could be used for other booleans, trying to use this boolean would be more complicated. Because it is not directly addressable, you would need to get the byte, and then extract the bit, before testing if it is 1 or 0. That means more instructions - hence slower performance.

If you have many booleans, and you want them to only use a single bit of memory EACH, you should use a BitArray. These are containers for single bits. They act like arrays of booleans.

ronalchn
  • 12,225
  • 10
  • 51
  • 61
  • 1
    Where can I learn more about this? – smartcaveman Sep 02 '12 at 09:34
  • Well, high level languages shield you from knowing some of the low level details. I suppose the best way to get an idea of how fast things execute, and why some things are implemented the way they are, is to learn Assembly Language. You don't really need to know it very well, but if you are aware of what kind of instructions are available etc, you will probably have enough knowledge to figure out why some things are done the way they are. Note: assembly generally has a one-to-one mapping with machine code, so you don't need to worry about machine code much. – ronalchn Sep 02 '12 at 09:37
  • I suppose it would also be useful to read up on the x86, and even the x64 architectures. There's some good information on pipelining, caching, branch prediction and other details. – ronalchn Sep 02 '12 at 09:38
  • Last question: Would you expect better or worse performance, if you replaced every reference to Boolean field in an application with an access call to a global BitArray? (Don't worry this isn't my intention, regardless of the answer. I'm just trying to conceptualize.) – smartcaveman Sep 02 '12 at 09:41
  • It would take less memory, but run slower. Having said this, it is HIGHLY UNLIKELY that the difference in memory or the difference in speed is noticeable AT ALL. This is because you are not likely to use the booleans THAT many times/second. It would probably only START to have a noticeable effect if you wanted to adjust something on the order of 1 million booleans. – ronalchn Sep 02 '12 at 09:48
  • BitArray can be very slow. We ended up creating a custom implementation which wrapped byte arrays and indexed the individual bits. – Tim Sep 23 '14 at 10:23
7

A byte is the smallest amount of addressable memory. The .NET team have chosen to use a byte to store a bool to simplify the implementation.

If you want to store a large number of bits more compactly you can look at BitArray.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • So, am I correct in my conclusion that there is no performance advantage to using a `bool` instead of bitwise operations on a `byte`-based enum? – smartcaveman Sep 02 '12 at 09:26
  • 1
    No, a bool taking up a whole byte might use more memory, but operations on it definitely execute faster! – ronalchn Sep 02 '12 at 09:28
  • @ronalchn, Why? - Specifically, with respect to an operation of checking for a specific flag. – smartcaveman Sep 02 '12 at 09:29
  • I mentioned it in my answer - because it is not "directly addressable" you need additional instructions to extract the bit of interest in order to read, set, compare, or apply and/or/xor operations to it. – ronalchn Sep 02 '12 at 09:30
4

Yes it requires a full byte of memory because that's the smallest addressable memory.

It would of course be possible to come up with a scheme where several bools can be put in the same byte, thus saving space. For more cases the overhead of such a solution would cost much more than gained.

If you have a lot of bits to store, a specialised bit vector (such as BitArray that Mark Byers metnions) can save precious space.

Anders Abel
  • 67,989
  • 17
  • 150
  • 217
1

If you think of 1 Byte as numeral value is 1 due to sizeof. So how can it say 1 bit ? Impossible, either it floors and return 0 and thats impossible or it returns 1 because it takes up for saving a byte because you don't save in bits.

But wether it's managed as a bit or a byte, I don't know.

In c++ you add to the variable-name a :1 to say it should be just 1 bit wide.

Joshua Behrens
  • 818
  • 13
  • 23