Value 1
| Value 2
| NOT (Value 1)
| AND (X)
| NAND
| OR (+)
| NOR
| XOR
|
0
| 0
| 1
| 0
| 1
| 0
| 1
| 0
|
0
| 1
| 1
| 0
| 1
| 1
| 0
| 1
|
1
| 0
| 0
| 0
| 1
| 1
| 0
| 1
|
1
| 1
| 0
| 1
| 0
| 1
| 0
| 0
|
XOR: If only one of the values is 1 (on) then returns 1
NOT: Acts on a single value only returning 1 for 0 and vice versa
NAND/NOR: NOT on AND/OR changes the value of the AND/OR using NOT
Interesting uses in encryption and programming
Swap variable A's and variable B's contents without a third variable
Example:
a = 123
b = 456
123 xor 456 = 435
a xor b = 435
Now, a xor 435 = 456 and b xor 435 = 123
So, we can switch the variables this way:
a=a xor b -> a=123 xor 456 = 435
b=a xor b -> b=456 xor 435 = 123 variable b now has variable a's value
a=a xor b -> a=435 xor 123 = 456 variable a now has variable b's value
| before | | | after | |
| a | b | function | a | b |
| 123 | 456 | a=a xor b | 435 | 456 |
| 435 | 456 | b=a xor b | 435 | 123 |
| 435 | 123 | a=a xor b | 456 | 123 |
No-Key Encryption
Without getting into the needs to make this secure or further information on the cipher security (read on One Time Pad and Vernam ciper)
Alice and Bob want to send each other a message without exchanging keys (without symetric encryption and without public key cryptography)
1) Alice takes the secret message and encrypts it with a key of her choice using the Vernam (XOR) encryption and sends the encrypted message to Bob.
2) Bob takes the encrypted message Alice sent him and encrypts it again using a key of his own choice and sends the message to Alice.
3) Alice removes her encryption and sends the message to Bob.
4) Bob removes his encryption and can read the message!
Lets look at it as if it was a box with two locking ears:

Plain text message, unencrypted

Alice encrypts the message (locks the box with her lock) with her key using XOR (Vernam) and sends to Bob (box only has Alice's lock)

Bob receives the encrypted message Alice sent and encrypts it again using his own key and sends it back to Alice (box has two locks)

Alice receives the encrypted message and removes her encryption and sends it back to Bob (box only has Bob's lock)

Bob receives the message, now only encrypted with his key, removes his encryption and reads the message
Lets look at a simple example in real life:
message = 1234
Alice's key = 1010
Bob's Key = 101
1234 xor 1010 = 1824
Alice encrypts an sends EM to Bob
1824 xor 101 = 1861
Bob receives EM and re-encrypts and sends EM to Alice
1861 xor 1010 = 1207
Alice receives EM and removes her encryption (lock) and sends to Bob
1207 xor 101 = 1234
Bob receives EM from Alice and removes his encryption (lock) to read the message