Appendix 2 - Logical Operators

This section explains the logic behind the various logical operators such as OR, XOR, AND. These operators basically compare the bits of two operands in various ways.

Xor

Xor stands for eXclusive OR. Basically it compares bits in the two operands with this in mind: "One or the other, but not both". What this means is that when comparing 2 bits, if one of them is 1, so long as the other is not also 1, the result will be 1. However, if both are 1, the result is 0. And if both are 0, the result is still 0, since both are the same.
This table may help. You'll see them throughout this section:

Bits of operand 1 1010
Bits of operand 2 1100
Result 0110

I know it's a little out there, but that shows an example xor instruction. Following this logic though, we can see why XORing a register with itself produces a 0. For example, lets xor al when it contains 141

AL 10001101
AL 10001101
Result 00000000

Because 0 xor 0 = 0, and 1 xor 1 = 0, there's no possible way that XORing something with itself would not equal 0.
Another use is toggling a bit or bits in a number. Watch what happens when we take the bit 0 and xor it 5 times:
0 xor 1 = 1
1 xor 1 = 0
0 xor 1 = 1
1 xor 1 = 0
0 xor 1 = 1

And so on. We can do the same with a whole number. Whichever bits you want toggled like we just did, XOR them with 1.

And

The logic behind AND is that in the result, only bits matched with themselves will turn out. See this table for example:

Bits of operand 1 1010
Bits of operand 2 1100
Result 1000

In the first bit, 1 was matched with itself, so the resulting bit is 1. However, 1 & 0 are not the same, so the result is 0.

Or

This is called an "inclusive OR". It basically means "if one OR the other is 1, the result is 1". So, the only way for a bit in the result to be 0 is if both bits are 0 in the operands

Bits of operand 1 1010
Bits of operand 2 1100
Result 1110

 


Originally posted at http://www.doorknobsoft.com/