## Sixteen Good Reasons To Love Xor ⊕

by Javantea
July 18, 2010

Xor is a binary operation that is versatile and not easily replaced. It represents a logical truth that is effective and seductive. Today I will give sixteen good reasons to love xor. If you are a computer scientist, programmer, or hacker and you don't use xor from time to time, you are doing something wrong.

But of course before I explain why to love it I should certainly explain it. Xor means exclusive or. It can be explained in English as either/or. That is to say that one is true or the other. If both are true, then the result of an xor is false. Likewise if both are false, xor is false. George Boole (1815–1864) discovered Boolean logic in his quest for a unifying theory of mathematics. Søren Kierkegaard (5 May 1813 – 11 November 1855) wrote Either/Or in his quest for understanding philosophic reasons behind how we live. He gives two examples of life, one that is defined by either, and the other that is define by or.

And now sixteen good reasons.

1. Xor enables binary addition of arbitrary length in a quite easy way.
2. Xor is the only non-destructive primary boolean operation with two operands.
3. Xor is an elementary binary operation.
4. Xor beats nand and nor in efficiency.
5. Xor can be used in one-time pad encryption.
6. Xor is often used in block cipher encryption.
7. Xor is a necessary part of stream cipher encryption.
8. Xor 0xff is equivalent to negation.
9. Xor 0x0 is equivalent to noop (aka nop).
10. Xor 0x80 flips the msb. Try this in python:
`	''.join(chr(ord(c) ^ 0x80) for c in 'this is a test')`
Xor 0x1 flips the lsb. Try this in python:
`	''.join(chr(ord(c) ^ 0x1)  for c in 'this is a test')`
Xor 0x2 flips the 2nd bit. Try this in python:
`	''.join(chr(ord(c) ^ 0x2)  for c in 'this is a test')`
Xor 0x4 flips the 3rd bit. Try this in python:
`	''.join(chr(ord(c) ^ 0x4)  for c in 'this is a test')`
Xor 0x8 flips the 4th bit. Try this in python:
`	''.join(chr(ord(c) ^ 0x8)  for c in 'this is a test')`
Xor 0x10 flips the 5th bit. Try this in python:
`	''.join(chr(ord(c) ^ 0x10) for c in 'this is a test')`
Xor 0x20 flips the 6th bit. Try this in python:
`	''.join(chr(ord(c) ^ 0x20) for c in 'this is a test')`
Xor 0x40 flips the 7th bit. Try this in python:
`	''.join(chr(ord(c) ^ 0x40) for c in 'this is a test')`

Related, try this in python:
```0x80 == 0x40 << 1 == 0x20 << 2 == 0x10 << 3 == 0x8 << 4 == 0x4 << 5 == 0x2 << 6 == 0x1 << 7
```
11. Xor is associative and commutative.
12. Xor's Venn diagrams look awesome.

13. Xor can be thought of as magical or not.
14. Xor can be generated using not, and, and or.
`x ^ y == (~x & y) | (x & ~y)`
15. Xor allows a user to swap two variables without a third variable.
```	x ^= y;
y ^= x;
x ^= y;```
16. The symbol for xor ⊕ cannot be found in Google.

Javantea Out.