1. Technology

Understanding How VB Logical Operators Work Bitwise

When is AND not AND? When you put the Byte on it!

By

You probably have a pretty good understanding of the logical operators And, Or, and Not already! 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!

Two new operators, AndAlso and OrElse, were added to the VB syntax with .NET. These are used to short-circuit a logical evaluation by evaluating the second expression only if it's necessary. It makes the code slightly more efficient, and can avoid some situations that might trigger an exception. This is a way of thinking about expression evaluation that is familiar to C programmers but new to VB programmers.

But there are some dark corners in some of the other logical operations. For example:


Dim A As Boolean
Dim B As Integer
A = True
B = 11
MsgBox(A Xor B)

What does the MsgBox display?

Give up? The answer is: -12

The reason is that in this case, the Xor logical operator is working on integers that are evaluated "bitwise". When you have a mixed expression, like the one above, the Boolean expression is evaluated as though it was a signed integer. This can be a useful technique. For example, you can use it to create your own "truth table" (sometimes called a flag array) using the individual bits in a number.

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 as a VB integer.

If you search Microsoft's site for a way to convert an integer into a displayable binary number, you might run into this code:


Function Bin(ByVal theInt As Integer) As String
    Const maxpower = 14
    Bin = ""
    If theInt < 0 Then
        Bin = Bin + "1"
    Else
        Bin = Bin + "0"
    End If
    For i = maxpower To 0 Step -1
        If theInt And (2 ^ i) Then
            Bin = Bin + "1"
        Else
            Bin = Bin + "0"
        End If
    Next
    Return Bin
End Function

Calling this function with 11 gives you the same binary digits shown earlier:


0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1

Actually, it's a lot easier than that. The same result can be found with the builtin VB.NET Convert.ToString(Int, Int) object. (Except that the leading 0's are not included.) In this case:


Convert.ToString(11, 2)

But understanding how Microsoft's code works is a great learning experience. The key is in this line of code:


If theInt And (2 ^ i) Then

The variable theInt contains the number to be converted to a string. Since the loop that this statement is in counts down from the maximum (For i = maxpower To 0 Step -1), the And is done for the each bit in the variable theInt from left to right.

In other words, the first iteration of the loop compares ...


0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1

... to ...


1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

... for a result of False.

But an interesting thing happens when we get to some 1 bits. The comparison of ...


0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1

... to ...


0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0

... gives you True.

Try it yourself:


Dim a As Integer
Dim b As Integer
a = 8
b = 11
If a And b Then
    Console.WriteLine("True")
Else
    Console.WriteLine("False")
End If

I can't find this documented anywhere at Microsoft's site. I'm sure it's there because they use this fact in their examples, but they certainly hide it well.

  1. About.com
  2. Technology
  3. Visual Basic
  4. Quick Tips
  5. Using Bitwise Logical Operators in Visual Basic

©2014 About.com. All rights reserved.