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?
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:
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:
Note: As with most VBScript operators, a
Nulloperand causes the operator to returnNull.Nullbehaves in an unusual way when used inside anIfconstruct.