You understand the logical operators! And, Or, and Not! If A is True and B is False, then A And B is False. And so forth. We're not going to go over the rest. You've probably seen it!
Did you also know that the two very rarely used VB 6 operators Imp and Eqv have disappeared in VB.NET, but two new operators, AndAlso and OrElse have appeared? To replace the missing operators, the VB.NET documentation suggests that you simply use the standard operators to accomplish the effect of Imp and Eqv. Here's Imp in code as an example.
Result = A Imp B 'True unless A True and B False
Result = (Not A) Or B 'Same as A Imp B
The two new operators in VB.NET, AndAlso and OrElse, are used to short-circuit a logical evaluation by evaluating the second expression only if it's necessary. This is a way of thinking about expression evaluation that is familiar to C programmers but new to VB programmers.
But that isn't what this DYK is all about. We're going to discuss what happens when something like this is evaluated (and why).
A = True
B = 11
MsgBox (A Xor B)
So ... what does the MsgBox display?
Give up? Answer: -12
And this is the answer in both VB 6 and VB.NET. (In VB.NET, you need to declare the A and B variables as Boolean and Integer.)
The point of the lesson is that logical operators work either on Boolean expressions (True or False, or something that evaluates to True or False) or integers that are evaluated "bitwise". When you have a mixed expression, as above, then the Boolean expression is evaluated as though it was a signed integer. This can be a useful technique (for example, to create your own "truth table" using the individual bits in a number) but it's even more useful if you happen to do it accidentally and you can't understand why your program is doing what it's doing. Once we work through this, you'll understand both this point ... and how to evaluate bitwise expressions.
When a logical operator evaluates two expressions "bitwise", they're converted into equal length integers and each bit is evaluated. So for the example above, True is the 16 bit number:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
The value 11 is:
0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1
And the XOR bitwise evaluation is:
1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 0
Which turns out to be -12.