Microsoft Access VBA Macro Programming

(Tina Sui) #1

Xor Operator


Xoris very similar toOr, except that True and True make False. Only True and False make
True, but there must be one True and one False. FalseXorTrue makes True, and TrueXor
False makes True.
Xorstands forExclusiveOr—thus, both values cannot both be True or False. The following
gives the value True:

MsgBox True Xor False

The following gives the value False:

MsgBox True Xor True

TheXoroperator works on binary arithmetic on the basis that 1Xor1 make 0, 1Xor 0
make 1, 0Xor1 make 1, and 0Xor0 make 0.
The top row of the following table represents the value of each binary bit going from bit 7
to bit 0. The two rows below it represent the binary equivalents of the numeric numbers on
the right of the table (columnn). The final row shows both the binary and numeric equivalents
when the two numbers areXored together. Each bit pair uses anXoroperator to achieve the
final result on the bottom line.

128 64 32 16 8 4 2 1 n
010101 0084
1 0 0 1 0 0 0 1 145
1 1 0 0 0 1 0 1 197

Each column of the preceding 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 contains the actual numbers.
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. 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 logicalXoris done on the two numbers, the result is 197 (128 + 64 + 4 +1). This
can be shown with the following VBA example:

MsgBox 8 4 Xor 145

This will give the result of 197. This result has an interesting property. If youXorthe result
with one of the numbers used, you will get the other number that was used to create the result.
The following will return the value 145:

MsgBox 8 4 Xor 197

78 Microsoft Access 2010 VBA Macro Programming

Free download pdf