37

Take the following code:

Sub Main()

    Dim i As Integer
    Dim b As Boolean

    i = 1
    b = i
    i = b
    Console.WriteLine(i)

    i = Convert.ToInt32(b)
    Console.WriteLine(i)

End Sub

This prints the following:

-1
1

Why is this?

(Just a joke :) You can get 0 too...

Int32.TryParse("True", i)
Console.WriteLine(i)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Prankster
  • 4,031
  • 5
  • 33
  • 44

8 Answers8

51

What you're seeing is a bit of legacy code showing its head.

At the heart of the matter is the VT_BOOL type. Visual Basic 6.0 used the VT_BOOL type (AKA VARIANT_BOOL) for its boolean values. True for a VARIANT_BOOL is represented with the value VARIANT_TRUE which has the integer value -1. During the conversion to .NET it was decided that when using Visual Basic conversion routines to convert a boolean value to an Integer value, Visual Basic 6.0 semantics would be maintained on the return value; it would be -1.

The first implicit conversion occurs with the b = i line. Under the hood this does an implicit conversion from integer to boolean. Any non-zero value is deemed to be true, therefore the resulting value is true.

However, the following line of code is doing an implicit conversion to an integer type.

i = b

Under the hood this uses one of the Visual Basic conversion routines (CType or CInt) to convert the value to an integer. As such Visual Basic semantics are in play and the value returned is -1.

The next interesting line is the Convert.ToInt32() line. This is using a .NET conversion routine which does not use Visual Basic semantics. Instead, it returns the underlying BCL representation for a true boolean value which is 1.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 4
    You might want to add some about about WHY VT_BOOL was used, and why it's value is -1. VB 6 only had 1 set of "and" and "or" operators, that did both logical and bitwise operations (most languages have 2 sets). This worked by making "and" implemented as "&" and have the literal default to -1. – Scott Wisniewski Apr 13 '09 at 21:07
  • That way "true and x" is non zero anytime "x" is non zero. – Scott Wisniewski Apr 13 '09 at 21:08
  • You can get a sense of the "why" in my answer to a mostly-unrelated question here: https://stackoverflow.com/a/46331671/3043 – Joel Coehoorn Sep 20 '17 at 21:21
  • And how does this explain why the compiler seems to be doing this for converting a bool to an int: `-(b > false)`? – Isaac Kleinman Nov 29 '17 at 16:46
33

Some languages consider boolean true to be -1 rather than 1. I'd have to do research to see why, as I don't recall.

In VB6, the constant True has the value -1.

However, Convert.ToInt32(Boolean) is documented as returning "The number 1 if value is true; otherwise, 0." That way, it's the same no matter which framework language you're using.

Edit: See the question boolean true -- positive 1 or negative 1

Community
  • 1
  • 1
Powerlord
  • 87,612
  • 17
  • 125
  • 175
  • 1
    Some languages use `-1` as `True`, because it is `0b11111111`, so every bit is the opposite of zero's binary representation, such that using a bitwise NOT operation on one would result in the other. – mbomb007 May 25 '16 at 21:09
  • 1
    In .NET 4.0 you get `CInt(true) = -1` and `Convert.Int32(true) = 1` – Pato Nov 27 '21 at 21:12
12

As to why -1 is used for True, I believe that it's because it is literally (NOT 0).

Start with zero, flip all the bits and then read it as two's complement--comes out negative one.

So since anything that is not False is True, and False is 0, the (NOT False) is represented by -1.

This may be just coincidence, though....

RolandTumble
  • 4,633
  • 3
  • 32
  • 37
  • 4
    That's exactly why VB uses -1 for True. :) – Daniel Lo Nigro Apr 19 '10 at 03:02
  • 1
    +1. It's interesting to note that if "true" is -1, the same operators can be used for Boolean logic and integer bit-masking. It's also interesting to note that the question of whether to short-circuit "and" and "or" is independent of the question of whether 'true' operands should be forced to a single value. – supercat Nov 24 '10 at 02:44
8

From MSDN Visual Basic documentation:

Type Conversions

When Visual Basic converts numeric data type values to Boolean, 0 becomes False and all other values become True. When Visual Basic converts Boolean values to numeric types, False becomes 0 and True becomes -1.

And for Convert.ToInt32(value):

retuns the number 1 if value is true; otherwise, 0.

So for your code:

i = 1
b = i // b becomes true
i = b // true = -1
Console.WriteLine(i)  // i is -1

i = Convert.ToInt32(b)  // Convert.ToInt32(true) = 1
Console.WriteLine(i)    // i is 1
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Arnold Spence
  • 21,942
  • 7
  • 74
  • 67
3

That's because in VB.NET, Boolean values are -1 for true and 0 for false by default. I am not sure why it prints as 1 the second time, though...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matt Grande
  • 11,964
  • 6
  • 62
  • 89
2

This is a dodgy answer but:

    Dim b As Boolean
    b = False
    Dim i As Integer
    i = IIf(b, 1, 0)
D.Alexander
  • 81
  • 1
  • 1
  • 7
2

"True" is an negation from the 0 value of a numeric data type!

Not(0) for non-signed types returns 1.

Not(0) for signed types returns -1.

I don't know your code, maybe your code performs a internal data conversion the second time.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Goldmoon
  • 136
  • 3
1

All numeric data types can used as Boolean! The result is dependent on the used data type.

Examples:

Dim i As Byte    ' Byte is non-signed!
Dim b As Boolean = True

i = b            ' Set first (lowest) bit of i (non-signed byte)
' i is now binary 0000 0001 = 1!


Dim i As SByte   ' SByte is signed!
Dim b As Boolean = True

i = b            ' Set all bits of i (signed byte)
' i is now FF (binary 1111 1111 = -1 !

Integer is signed, True to Integer -> -1.

UInteger is non-signed, True to UInt -> 1.

And so on...

A false value clears the highest bit in signed numerics, and lowest in non-signed numerics.

Therefore False is 0 in all numeric data types.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Goldmoon
  • 136
  • 3