Reputation: 4079
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)
Upvotes: 38
Views: 78240
Reputation: 81
This is a dodgy answer but:
Dim b As Boolean
b = False
Dim i As Integer
i = IIf(b, 1, 0)
Upvotes: 2
Reputation: 136
"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.
Upvotes: 2
Reputation: 136
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.
Upvotes: 1
Reputation: 22272
From MSDN Visual Basic documentation:
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
Upvotes: 8
Reputation: 12157
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...
Upvotes: 3
Reputation: 754575
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.
Upvotes: 52
Reputation: 4703
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....
Upvotes: 12
Reputation: 88796
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
Upvotes: 33