13

I know that a boolean value is 1 byte (8 bits long) But I would like to know is what is its binary representation. e.g. decimal => binary 4 => 100 (0000 0100) 8 => 1000 (0000 1000) bool value => ???

emm
  • 131
  • 1
  • 1
  • 3
  • I can't think of a scenario (other than some interop where you are forced to) where it's a good idea to treat booleans as integers. – SWeko Jul 26 '10 at 14:38
  • 2
    You can use BitArray if you wanted to interrogate the binary values. You cannot use watch, but you can use .Get[x] to get each bits value (true - 1, false - 0). You can also use Convert.ToByte() to convert the bool type to byte. – gooch Jul 26 '10 at 14:41
  • @gooch: `Convert.ToByte(bool)` just does the equivalent of `return (byte)(value ? 1 : 0);` so doesn't actually prove anything about the underlying representation. It just so happens that the underlying representation is the same. – LukeH Jul 26 '10 at 15:01
  • Yes, that is correct. The BitArray can be used for interrogation. The ToByte was more than emm was asking. – gooch Jul 26 '10 at 15:04
  • @gooch: Using `BitArray` doesn't prove anything about the underlying representation of `bool` either. `BitArray` uses the individual bits of an `int[]` array to store it's values, and the `Get` method then does the equivalent of `return (requiredBitOfRequiredArrayElement == 1);`. – LukeH Jul 26 '10 at 15:11

5 Answers5

20

bool is a built-in basic type in C#. Any underlying representation would be an implementation detail.

The C# 4.0 Language Specification states in section 4.1.8:

The bool type represents boolean logical quantities. The possible values of type bool are true and false.

No standard conversions exist between bool and other types. In particular, the bool type is distinct and separate from the integral types, and a bool value cannot be used in place of an integral value, and vice versa.

In the C and C++ languages, a zero integral or floating-point value, or a null pointer can be converted to the boolean value false, and a non-zero integral or floating-point value, or a non-null pointer can be converted to the boolean value true. In C#, such conversions are accomplished by explicitly comparing an integral or floating-point value to zero, or by explicitly comparing an object reference to null.

If we take this one level deeper and see how the corresponding type is specied in the Common Intermediate language (CIL) we will see that a CLI Boolean type occupies 1 byte in memory. The Common Language Infrastructure (CLI) specification says in Partition III, section 1.1.2:

A CLI Boolean type occupies 1 byte in memory. A bit pattern of all zeroes denotes a value of false. A bit pattern with any one or more bits set (analogous to a non-zero integer) denotes a value of true.

However, this is specified on another level and from within C# you should not have to care; even if a future version of the CLI specification might change the representation of the boolean type, or if the C# compiler decided to map a bool in C# to something different, your C# code would still have the same semantics.

Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
8

Here's a quick bit of code that demonstrates the underlying representation of bool, on the current platform wherever it happens to be running:

var x = new NotAGoodIdea();

x.TheBool = true;
Console.WriteLine(x.TheByte);    // 1

x.TheBool = false;
Console.WriteLine(x.TheByte);    // 0

// ...

[StructLayout(LayoutKind.Explicit)]
public struct NotAGoodIdea
{
    [FieldOffset(0)]
    public bool TheBool;
    [FieldOffset(0)]
    public byte TheByte;
}

(Note that although 1 appears to represent true and 0 appears to represent false, this is just an implementation detail. You shouldn't rely on this detail, or assume that it will remain consistent across different versions and/or implementations, or even that the current platform always uses the same consistent representation.)

EDIT...

The ECMA CLI spec (partition III, section 1.1.2) is pretty clear about the allowable representations of the Boolean type:

1.1.2 Boolean data type

A CLI Boolean type occupies 1 byte in memory. A bit pattern of all zeroes denotes a value of false. A bit pattern with any one or more bits set (analogous to a non-zero integer) denotes a value of true.

It appears that the current Microsoft CLR adheres to the ECMA spec in allowing multiple representations of true. The following example displays a single "False" line (for 0) followed by 255 lines of "True":

// re-use the NotAGoodIdea struct from the previous example
var x = new NotAGoodIdea();

for (int i = 0; i < 256; i++ )
{
    x.TheByte = (byte)i;
    Console.WriteLine(x.TheBool);
}
LukeH
  • 263,068
  • 57
  • 365
  • 409
  • 3
    Note though that `object.Equals()` doesn't consider two `bool` that are internally different as equal. Arguably a bug with `object.Equals()` and certainly unlikely to come up with sane code, but there is at least that one case where while the CLI spec considers something true, the BCL does not. – Jon Hanna Jan 15 '14 at 12:09
6

I'm not contradicting 0xA3's answer, but if you use:

BitConverter.GetBytes(true);
BitConverter.GetBytes(false);

You'll get a byte array of { 1 } and { 0 }. In other words, the binary values would be 00000001 and 00000000.

This doesn't mean that's how .NET handles booleans in memory - it's just how it converts them to byte arrays.

Jon B
  • 51,025
  • 31
  • 133
  • 161
0

Almost all languages/environments (not only .NET) implement true as equivalent to the integral value 1, and false equal to 0.1)

However, there’s one important exception, namely VB6, which had true equal to –1. This made quite a few things difficult for the migration to .NET, because the loose type system of VB6 allowed to mix integers and booleans in the same expression, and 2 And True meant something else in VB6 than in VB.NET.


1) Although many systems allow an implicit conversion of any numeric value unequal to 0 to true in a boolean context. Some (especially dynamic) languages even go further, and say that all objects except for special objects (e.g. None, empty array, the list goes on …) equal true.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 3
    In older system, -1 (0xffffff) and 0 are also used. – H H Jul 26 '10 at 14:40
  • 5
    I thought that C treated 0 as false, and anything else as true? – Oded Jul 26 '10 at 14:40
  • @Oded: yes, true == ! false. Same for conversions form pointer. – H H Jul 26 '10 at 14:44
  • @Oded: yes, that’s why I wrote “implement” instead of “treats”, and if you write `true` in C++ (`TRUE` in C? Dunno …) this will be implemented by the value `1`. – but notice the added remark. – Konrad Rudolph Jul 26 '10 at 14:44
0

Typically Boolean values are represented by false being all zeros and true being anything else. For simplicity this is normally -1 (all bits on of a signed integral type) due to Two's Complement.

Matthew Whited
  • 22,160
  • 4
  • 52
  • 69