# Thread: TRUE = -1 in VBA (2000 SP3)

1. ## TRUE = -1 in VBA (2000 SP3)

Out of curiosity, anybody know why a TRUE condition evaluates to -1 in VBA? If you enter "Msgbox 1 * (2 >1)", I would have expected to get +1. That's what you'd get, of course, if you enter "=1*(2>1)" in a cell. FALSE does evaluate to zero, thankfully.

2. ## Re: TRUE = -1 in VBA (2000 SP3)

I could be completely off base but I think it has to do with the fact that the last bit of a numeric field normally indicates the sign, with a 0 indicating a positive number and a 1 indicating a negative number. When you translate a 1 bit boolean into decimal, all it sees is a 1 in the last bit so it's a negative number. -1 was probably chosen as a convenience.

Conversely, I believe that 0 translates to FALSE, and any nonzero translates to TRUE.

3. ## Re: TRUE = -1 in VBA (2000 SP3)

In Excel formulas, TRUE is equivalent to +1 (and FALSE to 0), but in VBA, like in most programming languages, TRUE is equivalent to -1. TRUE is represented as a number all whose bits are "on", FALSE as a number all whose bits are "off". The number with all bits "on" also represents the number -1 in the coding most used by computers (2's complement). As Chipshot remarks, any non-zero value is also considered to be TRUE, but you have to watch out if you combine values with AND and OR; that will only work correctly with "true" TRUE values <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

4. ## Re: TRUE = -1 in VBA (2000 SP3)

.. which why CBool() is occasionally useful in VBA.

5. ## Re: TRUE = -1 in VBA (2000 SP3)

As noted in other replies, this is because a 32-bit binary number with all bits "On" (1) equates to -1, while the same number with all bits "Off" (0) equates to zero. This is more obvious using a simple decimal-to-binary conversion function:

<pre>? DecToBin(0)
00000000000000000000000000000000

? DecToBin(-1)
11111111111111111111111111111111</pre>

The DecToBin function used here was adapted from:
MSKB 109260, How to Convert a Decimal Number to a Binary Number in a String

Public Function DecToBin(ByVal lngDec As Long) As String

Const MAXLEN = 30
Dim strBin As String
Dim n As Long

If lngDec < 0 Then
strBin = "1"
Else
strBin = "0"
End If

For n = MAXLEN To 0 Step -1
If lngDec And (2 ^ n) Then
strBin = strBin & "1"
Else
strBin = strBin & "0"
End If
Next

DecToBin = strBin

End Function

As article notes: "This program converts negative decimal numbers into the internal twos-complement binary format used by Basic. In that format, the left-most binary digit (the thirty-second digit in a long integer) will always be 1 for a negative number and 0 for a positive number." If you convert +1 to binary:

<pre>? DecToBin(1)
00000000000000000000000000000001</pre>

It'll be seen only one bit is "on". Anyway, that's why "True" in VB/VBA equates to -1 instead of positive +1, if this makes any sense....

HTH

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•