Take the 2-minute tour ×

What will this print, and why?

Sub Main()

    Dim i As Integer
    Dim b As Boolean

    i = 1
    b = i
    i = b

    i = Convert.ToInt32(b)

End Sub

I know what it prints (check revision history or run it yourself to see). I'm interested in the "why".

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

Int32.TryParse("True", i)
share|improve this question

7 Answers 7

up vote 26 down vote accepted

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




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.


share|improve this answer
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

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

share|improve this answer

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

share|improve this answer
That's exactly why VB uses -1 for True. :) –  Daniel Lo Nigro Apr 19 '10 at 3:02
+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 2:44

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
share|improve this answer
Add a link for those doc quotes and I'll upmod. –  Joel Coehoorn Apr 13 '09 at 20:53

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

share|improve this answer

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


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.

share|improve this answer

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

share|improve this answer

Your Answer


