Lumi
Lumi

Reputation: 15304

Boolean Not operator in VBScript

Consider the following two conditionals involving bitwise comparisons in VBScript:

If     1 And 3  Then WScript.Echo "yes" Else WScript.Echo "no"
If Not(1 And 3) Then WScript.Echo "yes" Else WScript.Echo "no"

I think the output should be:

yes
no

But the actual output is:

yes
yes

Wait a second, the Not operator is supposed to perform logical negation on an expression. The logical negation of true is false, as far as I know. Must I conclude that it doesn't live up to that promise? How and why and what is going on here? What is the rationale, if any?

Upvotes: 11

Views: 55756

Answers (2)

Lumi
Lumi

Reputation: 15304

I found the answer to my question on Eric Lippert's blog: Not Logical Is VBScript. Turns out the Not operator, like its brethren, is not actually a boolean operator.

And, Or, Not and Xor are filed under Logical Operators on MSDN.

But Eric Lippert labels them as bitwise, and that's a better description than just logical as on MSDN, because logical doesn't say how they're actually working, so people like me get fooled into believing they're boolean operators, which they're not. And this is a major trap.

I would have to rewrite my statements as follows in order to have them do what I want:

If     (1 And 3) > 0  Then WScript.Echo "yes" Else WScript.Echo "no"
If Not((1 And 3) > 0) Then WScript.Echo "yes" Else WScript.Echo "no"

This prints yes and then no as it should.

Update: While the above does seem to work for this case, it's definitely not the way to go. The problem is in understanding what it is that I want to check for. The above code doesn't make it clear. A bitwise comparison and a numeric comparison for greater than 0 on the result are not equivalent to a boolean check.

Consider the following code, which adopts Salman A's CBool suggestion as the final solution to this problem:

Option Explicit
Dim a, b
a = -3 : b = -2

If  a And b       Then WScript.Echo "ja" Else WScript.Echo "nein" ' bad
If (a And b) > 0  Then WScript.Echo "ja" Else WScript.Echo "nein" ' bad
If CBool(a And b) Then WScript.Echo "ja" Else WScript.Echo "nein" ' good

Upvotes: 6

Salman Arshad
Salman Arshad

Reputation: 272266

The VBScript AND operator performs a logical AND operation if both operands are boolean (True, False) -- somewhat like the C (style) language && operator.

If both operands are numeric, it will instead perform a bitwise AND operation -- somewhat like the C language & operator.

If the operands are of mixed types then the boolean value is cast to a number -- False = 0, True = -1 (surprise!) followed by a bitwise AND operation.

So your example evaluates as follows:

'   1 And 3
' = &h0001 And &h0003 <- bitwise AND
' = 1
If 1 Then WScript.Echo "yes" Else WScript.Echo "no" ' Yes

'   Not(1 And 3)
' = Not(1) <- see above
' = Not(&h0001) <- bitwise NOT
' = &hFFFE
If -2 Then WScript.Echo "yes" Else WScript.Echo "no" ' Yes

In case you are wondering how the VBScript NOT operator works, it performs logical negation on a boolean operand like the C language ! operator and bitwise complement on a numeric operand like the C language ~ operator.

In case you want to force logical operations on operands, use the VBScript CBool function to cast the operands:

If Not(CBool(1) And CBool(3)) Then WScript.Echo "yes" Else WScript.Echo "no" ' no

Note: As with most VBScript operators, a Null operand causes the operator to return Null. Null behaves in an unusual way when used inside an If construct.

Upvotes: 15

Related Questions