home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Advanced Visual Basic Techniques
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471188816
Publication Date: 06/01/97

Search this book:
 
Previous Table of Contents Next


Many programs use two special masks: a mask with no bits set and a mask with every bit set. A mask with no bits set represents an object with none of the defined features. This mask can be defined by giving it the value 0.

A mask that has every bit set represents an object with every defined feature. Because Visual Basic’s bitwise Not operator switches the values of every bit in an integer value, this value is the bitwise negation of the value with no bits set.

Public Const VEH_NoFeatures = 0        ‘ No features.
Public Const VEH_AllFeatures = Not VEH_NoFeatures   ‘ Every feature.

Combining Bit Masks

A program can manipulate bit masks using the bitwise operators And, Or, Not, and Xor. The bitwise And of two integer values A and B contains a one in any position where the corresponding bits of A and B are both one.

    00...01101
And 00...01010
    00...01000

The bitwise Or of two integer values A and B contains a one in any position where the corresponding bit of either A or B is one.

    00...01001
Or  00...01010
    00...01011

The bitwise Not or bitwise negation of an integer value A contains a one in every position where A has a zero. It has a zero in each position where A has a one.

Not 00...01010
    11...10101

Finally, the bitwise Xor or exclusive or of two values A and B contains a one in any position where the corresponding bit of either A or B but not both is a one.

     00...01011
Xor  00...00010
     00...01001

To define a value that represents more than one bit mask feature, a program can combine the separate bit masks using the bitwise Or operator. The following code creates a value representing a vehicle with four-wheel drive, anti-lock brakes, and dual airbags. The vehicle does not have any other features such as power steering or automatic transmission.

Dim car_features As Integer
    car_features = _
        VEH_4WD Or _
        VEH_ABS Or _
        VEH_Airbag1 Or _
        VEH_Airbag2

Once a program has defined bit mask values for a set of objects, it must be able to determine what the values mean. It can do this using the bitwise And operator. If the program uses And to combine a value with a mask representing a single feature, the result will be zero unless the value has the same bit set as the mask. Continuing the previous example, car_features And VEH_4WD = VEH_4WD while car_features And VEH_Automatic = 0.

In Visual Basic, nonzero integer values are treated as true in logical comparisons and zero values are treated as false. That means the program can use bit masks to easily identify objects that meet certain conditions. For example, suppose a program has an array Features that contains bit masks describing a group of vehicles. The program could use the following code to locate all of the vehicles that have four-wheel drive.

Dim i As Integer
    For i = LBound(Features) To UBound(Features)
        If Features(i) And VEH_4WD Then
            MsgBox “Vehicle” & Str$(i) & “ has four-wheel drive.”
        End If
    Next i

A program can combine masks using the Or operator to identify objects that have one or more of a set of features. The following code identifies vehicles that have four-wheel drive or anti-lock brakes:

Dim i As Integer
    For i = LBound(Features) To UBound(Features)
        If Features(i) And (VEH_4WD Or VEH_ABS) Then
            MsgBox “Vehicle” & Str$(i) & _
                “ has four-wheel drive OR anti-lock brakes.”
        End If
    Next i

In complicated tests like this one, a program must use parentheses to force Visual Basic to evaluate the expression in the correct order. Without the parentheses, Visual Basic treats the following two statements equivalently:

Features(i) And VEH_4WD Or VEH_ABS
(Features(i) And VEH_4WD) Or VEH_ABS

Because the result of the first half of the second equation is combined with VEH_ABS using the bitwise Or operator, the result will have at least the same bit set as in the mask VEH_ABS. This means the result will always be nonzero so it will be treated as logically true no matter what value Features(i) has.

A program must take special care when using both the logical and bitwise meanings of an operator. For instance, the following code seems to identify vehicles that have both four-wheel drive and anti-lock brakes:

If (Features(i) And VEH_4WD) And _
   (Features(i) And VEH_ABS) Then...

Actually, Visual Basic treats all of the And operators in this example as bitwise operators. That means this expression will return true only if Features(i), VEH_4WD, and VEH_ABS all have at least one bit set to one in the same position. Because VEH_4WD and VEH_ABS are single feature bit masks, they have no set bits in common so this will never happen.

This code must be modified so the middle And operator is logical rather than bitwise. The following code uses the CBool conversion function to convert the first and last parts of the expression from bit values into Boolean values. Then the middle And operator correctly identifies vehicles that have both four-wheel drive and anti-lock brakes.

Dim i As Integer
    For i = LBound(Features) To UBound(Features)
        If CBool(Features(i) And VEH_4WD) And _
           CBool(Features(i) And VEH_ABS) _
        Then
            MsgBox “Vehicle” & Str$(i) & _
                “ has four-wheel drive AND anti-lock brakes.”
        End If
    Next i

Using the Not operator a program can find objects that do not have a specific feature. Again the program must take care to avoid confusing logical and bitwise operators. The bitwise negation of a number almost always has at least one bit set so it will be regarded as logically true. What is needed in this case is the logical negation of the value. The following code finds vehicles that do not have automatic transmissions:

Dim i As Integer
    For i = LBound(Features) To UBound(Features)
        If Not CBool(Features(i) And VEH_Automatic) Then
            MsgBox “Vehicle” & Str$(i) & _
                “ does NOT have an automatic transmission.”
        End If
    Next i

Using other combinations of operators a program can identify objects with any combination of features. It could locate vehicles with anti-lock brakes and a driver-side airbag that do not have automatic transmissions, power windows, or remote keyless entry. In elaborate cases such as this, the program should include lots of parentheses to force Visual Basic to apply the operators in the correct order. It should also use the CBool function where necessary to convert the results of bitwise operators into logical values.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.