*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.

- Xor enables binary addition of arbitrary length in a quite easy way.
- Xor is the only non-destructive primary boolean operation with two operands.
- Xor is an elementary binary operation.
- Xor beats nand and nor in efficiency.
- Xor can be used in one-time pad encryption.
- Xor is often used in block cipher encryption.
- Xor is a necessary part of stream cipher encryption.
- Xor 0xff is equivalent to negation.
- Xor 0x0 is equivalent to noop (aka nop).
- 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

- Xor is associative and commutative.
- Xor's Venn diagrams look awesome.

- Xor can be thought of as magical or not.
- Xor can be generated using not, and, and or.
x ^ y == (~x & y) | (x & ~y)

- Xor allows a user to swap two variables without a third variable.
x ^= y; y ^= x; x ^= y;

- The symbol for xor ⊕ cannot be found in Google.

Javantea Out.

Permalink-
Leave a Reply

## Comments: 0

## Leave a reply »