XOR - QB64 Wiki

# XOR

The XOR numerical operator returns the bitwise exclusive OR.

return% = value1% XOR value2%

• Given two integer-typed values XOR returns a new integer with the property that the result in each bit position is 1 if the two bits are different, and 0 if they are the same. This can be used to alternate bit states every time XOR is used.
• Returns 0 if both of the bit values compared are off or on.
• Returns 1 if only one of the two values has a corresponding bit on.
• XOR is used to turn off bits that are on or turn on bits that are off. Like toggling the bit values.
• Byte (8 bit) values can be from 0 to 255. 0 = all bits off. 255 = all bits on.

The results of the bitwise logical operations, where A and B are operands, and T and F indicate that a bit is set or not set:
Operands Operations
A B NOT B A AND B A OR BA XOR BA EQV BA IMP B
T T F T T F T T
T F T F T T F F
F T F F T T F T
F F T F F F T T
Note that the Relational Operations return negative one (-1, all bits set) and zero (0, no bits set) for true and false, respectively.
This allows relational tests to be inverted and combined using the bitwise logical operations.

Example 1: Comparing the results of XOR, AND and OR value comparisons on one byte number values up to 255.

' bit# 76543210 also the exponent of 2 value on ' -------- a% = 233 ' binary 11101001 values used b% = 111 ' binary 01101111 ' -------- result1% = a% XOR b% ' 10000110 binary Result1% = 134 result2% = a% AND b% ' 01101001 binary Result2% = 105 result3% = a% OR b% ' 11101111 binary Result3% = 239

Explanation: Look down the columns to see how the bits are changed. The three values returned by XOR, AND and OR are relational. If you add the values returned by XOR(134) and AND(105) the result is the value of the OR operation, 239.

Example 2: Using XOR to turn a parallel port's data register bits on or off

Explanation: Only the first 3 Bits are affected. If on then turned off. If off then turned on. Each time the OUT statement above is used, the bit values alternate. That is because the value of 7 compares the bit's status to an on only value.