Microsoft Access VBA Macro Programming

(Tina Sui) #1
Each column of this table shows a binary bit based on an eight-bit number. The bit values are
shown in bold across the top. The right-hand column (n) contains the actual decimal values.
Bits are counted from the right to left, starting at bit 0 and ending at bit 7 for a single-byte
number. Notice that the values of each bit increase in powers of 2. Bit 0 is represented by the
value of 1, and bit 7 is represented by the value of 128. The first number is 84, so bit 6, bit 4,
and bit 2 are all set. If you add 64 + 16 + 4, this comes to 84. The second number is 145, so
bit 7, bit 4, and bit 0 are set. If you add 128 + 16 + 1, this comes to 145.
When a logicalAndis done on the two numbers, the result is 16. This is because the only
bit where both numbers have a value is bit 4. This can be shown with the following example:

MsgBox 8 4 And 145

This will give the result of 16.
This strange binary arithmetic is generally used for testing whether bits are set within a
number or for masking purposes. Masking sets the values of certain bits to True or False
within a number. To do this, a “mask” number isOred with the target number, and the bit in
the mask will be set to the mask value. For example, if you want bit 7 to be set to 1, then you
Or your target number with 128 (bit 7 value) and bit 7 in the target number is then set to True,
regardless of what values the other bits have.
Also, for example, you could have a variable that uses eight bits to hold various information
on something, almost like properties. Each bit may represent a certain setting. If bit 4 represents
a certain value and you want to see if it is set, all you do isAndit with 16, which is the binary
number for bit 4. If the bit is set, it will give a value of 16; otherwise, it will give a value of 0.
This acts totally independently of values that the other bits are set to.

Not Operator


TheNotoperator performs a logicalNoton two numbers or expressions. It basically inverts
the bits within a number. If a bit is set to 0, then it becomes 1; if it is set to 1, it becomes 0.

MsgBox Not ( 2 = 3 )

This will give the result True because 2 does not equal 3 (which is False), but the Not statement
then inverts the bits and makes it True.

Or Operator


TheOroperator works on the basis that either two values can be True (nonzero) or one can
be True and the other False (zero). The following returns True because one of the values is
True (True and False are built in variables within VBA):

MsgBox True Or False

76 Microsoft Access 2010 VBA Macro Programming

Free download pdf