Last updated on June 9, 2025
Seeing Code with New Eyes
In this installment of the Demystifying Code series, we’re diving into XOR, one of the most oddly powerful tools in computing. XOR is everywhere: in cryptography, in RAID arrays, in error detection, and even in data hiding.
And yet, it’s based on one of the simplest logic gates.
What is XOR?
XOR (exclusive or) compares two bits:
- If the bits are different, it returns 1
- If they’re the same, it returns 0
Here’s a truth table:
A | B | A ^ B
--|---|------
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 0
In other words, XOR highlights the differences. This deceptively simple behavior gives it powerful capabilities.
🔐 XOR for Obfuscation
Let’s start with a quick example. Suppose you have a message and you XOR each byte with a fixed key:
char msg[] = "secret";
for (int i = 0; msg[i]; i++) {
msg[i] ^= 0xAA;
}
The result is garbage — unreadable. But XOR it again with the same key, and the original message comes back.
This is reversible obfuscation, and it works because:
XOR(XOR(x, key), key) == x
XOR is its own inverse.
🧠 XOR for Recovery: Reconstructing Missing Data
XOR isn’t just for hiding data. It can rebuild it.
Let’s say you XOR three numbers:
int a = 123;
int b = 456;
int c = 789;
int xor_sum = a ^ b ^ c; // Store this value
Now suppose you lose b. You can recover it like this:
int recovered_b = a ^ c ^ xor_sum;
Try it:
123 ^ 789 ^ (123 ^ 456 ^ 789) = 456
This trick is used in RAID 5. If one disk is lost, XOR the others and the parity to recover the missing data.
📱 A Real-World Demo You Can Try
Want to see it work? Try this with three phone numbers:
Your number: 1012348765
Friend's number: 5551234567
Parent's number: 7779876543
XOR all three:
Result = 3103872997
You can even do this with a scientific calculator.

Now pretend you forgot your number. XOR the other two and the result:
5551234567 XOR 7779876543 XOR 3103872997 = 1012348765

It really works. The math checks out.
Why This Matters
- XOR can encrypt and decrypt data with the same operation
- XOR can recover lost data when stored with parity
- XOR is behind many tools you’ll use every day, whether you realize it or not
This article shows how logic turns into power.
