XOR Operation Explained Simply (With Examples)
Learn what XOR means in logic and computing, how the truth table works, and how to evaluate XOR expressions with practical binary examples.
XOR stands for “exclusive OR,” and it is one of the most useful operations in computer science. It returns true only when the two inputs are different. If both inputs match, the result is false. That single rule powers everything from encryption to error detection, and once it clicks, you will start seeing it everywhere. To test bit patterns directly, try the XOR Calculator.
The XOR Truth Table
The truth table is the fastest way to internalize how XOR behaves. For two single-bit inputs A and B:
| A | B | A XOR B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Read it like this: when A and B disagree, the output is 1. When they agree, the output is 0. That is the entire operation in four rows.
Why It Is Called Exclusive OR
Regular OR (the inclusive kind) returns true when at least one input is true — including the case where both are true. XOR is stricter. It returns true only when exactly one input is true. The moment both inputs are true, the result flips to false. That is the “exclusive” part: it excludes the both-true case that regular OR allows.
If you are working through logic gate problems, the Logic Gate Calculator can help you compare OR, AND, NAND, and XOR side by side.
Worked Example With 8-Bit Numbers
Let us walk through a full example using 8-bit representations. Say you want to XOR decimal 77 and decimal 43.
First, convert both to 8-bit binary:
77 = 0100110143 = 00101011
Now XOR each bit position, column by column:
01001101 (77)
^ 00101011 (43)
----------
01100110 (102)
Each column follows the same rule: different bits produce 1, matching bits produce 0.
The result is 01100110, which equals decimal 102. So 77 XOR 43 = 102. You can verify this with the Binary Calculator.
XOR Properties That Make It Special
XOR has a handful of mathematical properties that make it far more powerful than it looks at first glance.
Self-inverse. XOR is its own undo button. If you XOR a value with a key, you can XOR the result with the same key to get the original value back. In notation: A XOR K XOR K = A. This property alone is the reason XOR shows up in so many cryptographic algorithms.
Commutative. The order of the operands does not matter. A XOR B always equals B XOR A, just like addition.
Associative. You can group XOR operations however you like. (A XOR B) XOR C equals A XOR (B XOR C). This means you can chain as many XOR operations together as you need without worrying about parentheses.
Identity element. XOR with zero does nothing: A XOR 0 = A. Zero is the identity element for XOR.
Self-cancellation. Any value XOR-ed with itself gives zero: A XOR A = 0. This ties directly into the self-inverse property and is the foundation of the swap trick discussed below.
XOR in the Real World
These properties are not just theoretical. They show up in practical systems every day.
Encryption basics (one-time pad). The simplest unbreakable encryption is the one-time pad. XOR your plaintext with a random key of the same length and the result is ciphertext that is impossible to crack without the key. To decrypt, XOR the ciphertext with the same key to recover the original. The self-inverse property is what makes this work.
Error detection (parity bits). When data travels over a network or gets written to disk, bits can flip. Parity checking XOR-s all the bits in a block to produce a single check bit. If any bit flips during transmission, the XOR of the block will no longer equal zero, signaling an error. RAID 5 storage uses this exact principle to survive a drive failure.
Swapping variables without a temporary. Here is a classic programming trick. You can swap two integer variables without allocating extra memory:
a = a XOR b
b = a XOR b
a = a XOR b
After those three lines, a and b have traded values. It works because of the self-inverse and associative properties. Most compilers optimize a standard swap just as well, but the XOR swap is a great exercise for understanding bitwise operations.
Toggling states in UI. If you have a boolean flag tracking whether a menu is open or closed, XOR-ing it with 1 flips the state every time: state = state XOR 1. The same idea applies to toggling individual bits in a bitmask, which is common in embedded systems and game development.
A Fast Mental Model
The simplest way to remember XOR is the phrase: “same gives 0, different gives 1.” That rule applies whether you are working with single booleans, 8-bit bytes, or 256-bit encryption keys. Once you have that down, the truth table, the properties, and the real-world applications all follow naturally.
Additional Worked Example: XOR for Simple Data Masking
Suppose you want to mask the ASCII character “H” (decimal 72) using the key value 42.
Convert both to binary:
72 = 0100100042 = 00101010
XOR them:
01001000 (72, 'H')
^ 00101010 (42, key)
----------
01100010 (98, 'b')
The masked result is 98, which corresponds to the ASCII character “b”. To recover the original, XOR the result with the same key:
01100010 (98, 'b')
^ 00101010 (42, key)
----------
01001000 (72, 'H')
You get 72 back — the original “H”. This is exactly how simple stream ciphers work: XOR each byte of plaintext with a corresponding key byte, and XOR the ciphertext with the same key to decrypt. The Binary Calculator can help you verify each step, and the Base Converter makes it easy to switch between decimal, binary, and hexadecimal representations.
Common XOR Misconceptions
“XOR is the same as addition.” XOR and addition produce the same result for single-bit inputs (0+0=0, 0+1=1, 1+0=1), but they diverge when both inputs are 1. Addition gives 2 (or 10 in binary, with a carry), while XOR gives 0 with no carry. XOR is sometimes called “addition without carry,” which is a useful mental model but not an exact equivalence.
“XOR encryption is secure on its own.” A one-time pad (XOR with a truly random key of equal length, used only once) is theoretically unbreakable. But in practice, people reuse keys, use short keys repeated over the plaintext, or use pseudo-random generators with predictable seeds. Reusing an XOR key is catastrophic: if an attacker has two ciphertexts encrypted with the same key, XOR-ing them together cancels out the key and reveals the XOR of the two plaintexts, which is often enough to recover both messages. Modern encryption (AES, ChaCha20) builds on XOR but adds layers of diffusion and confusion that simple XOR lacks.
“XOR only works on binary numbers.” XOR operates on bits, but it can be applied to any data that can be represented as bits — which is everything in a computer. Text, images, audio files, and database records are all sequences of bits under the hood. When you “XOR a file,” you are XOR-ing each byte of the file with a corresponding key byte.
XOR in Programming Languages
Different languages use different syntax for XOR, but the operation is identical everywhere:
| Language | XOR Operator | Example |
|---|---|---|
| C / C++ / Java | ^ | int result = 77 ^ 43; |
| Python | ^ | result = 77 ^ 43 |
| JavaScript | ^ | let result = 77 ^ 43; |
| Rust | ^ | let result = 77 ^ 43; |
| Go | ^ | result := 77 ^ 43 |
In all these languages, 77 ^ 43 returns 102, matching our earlier worked example. The ^ symbol is consistent across most C-family languages. Be careful not to confuse it with exponentiation (Python uses ** for powers, and JavaScript uses Math.pow() or **).
XOR-Based Interview Questions
XOR appears frequently in coding interviews because it enables elegant solutions to problems that seem hard at first glance.
Find the single non-duplicate in an array. Given an array where every element appears exactly twice except one, find the unique element. The brute-force approach uses a hash set, but XOR solves it in O(n) time with O(1) space. XOR all elements together: the duplicates cancel each other out (since A XOR A = 0), leaving only the unique value.
For example, given [4, 1, 2, 1, 2]:
4 ^ 1 ^ 2 ^ 1 ^ 2 = 4 ^ (1 ^ 1) ^ (2 ^ 2) = 4 ^ 0 ^ 0 = 4
The answer is 4. This works because XOR is both commutative and associative, so the order of operations does not matter.
Detect a missing number. Given an array containing n-1 of the integers from 1 to n, find the missing one. XOR all array elements with all integers from 1 to n. The matching pairs cancel out, leaving only the missing number. This avoids potential integer overflow issues that a summation approach might encounter with very large n.
Use the XOR Calculator to verify these solutions with your own test cases.
Related Reading
- How GCF and LCM Work covers another fundamental operation in discrete mathematics that shares the pattern of combining values through a defined rule.
- How to Calculate Percentage offers a complementary math skill for situations where bitwise operations give way to arithmetic reasoning.
The Takeaway
XOR is deceptively simple — a single rule about difference — but its mathematical properties make it one of the most versatile tools in computing. From securing data with encryption to catching transmission errors to clever bit tricks in everyday code, XOR punches well above its weight. To experiment with your own values, head over to the XOR Calculator.
Sources
- MDN Web Docs. “Bitwise XOR (^).” Mozilla Developer Network. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_XOR
- GeeksforGeeks. “Bitwise XOR Operator in Programming.” https://www.geeksforgeeks.org/bitwise-xor-operator-in-programming/
- Paar, Christof, and Jan Pelzl. Understanding Cryptography: A Textbook for Students and Practitioners. Springer, 2010. Chapter 2: Stream Ciphers.
Frequently Asked Questions
What does XOR mean and how does it work? +
XOR stands for exclusive OR. It returns 1 (true) only when the two input bits are different, and 0 (false) when they are the same. The truth table has four rows: 0 XOR 0 = 0, 0 XOR 1 = 1, 1 XOR 0 = 1, 1 XOR 1 = 0. The simple way to remember it: same gives 0, different gives 1.
What is the difference between XOR and regular OR? +
Regular OR (inclusive OR) returns true when at least one input is true, including the case where both are true. XOR (exclusive OR) returns true only when exactly one input is true — it excludes the both-true case. XOR models situations where having both conditions true simultaneously is either impossible or should produce a different result.
How is XOR used in encryption? +
XOR is central to stream ciphers and one-time pads. Each byte of plaintext is XOR-ed with a corresponding key byte to produce ciphertext. To decrypt, XOR the ciphertext with the same key — the self-inverse property (A XOR K XOR K = A) perfectly restores the original. Reusing the same XOR key is catastrophic for security, since XOR-ing two ciphertexts cancels the key and reveals the relationship between the plaintexts.
What are the mathematical properties of XOR that make it useful? +
XOR is commutative (A XOR B = B XOR A), associative ((A XOR B) XOR C = A XOR (B XOR C)), self-inverse (A XOR K XOR K = A), has an identity element of zero (A XOR 0 = A), and self-cancels (A XOR A = 0). These properties enable elegant solutions like variable swapping without a temporary variable and identifying the single non-duplicate in an array.
How do you perform XOR on binary numbers? +
Convert both numbers to binary, then apply the XOR rule column by column: matching bits produce 0, different bits produce 1. For example, 77 (01001101) XOR 43 (00101011) = 01100110, which equals decimal 102. The XOR symbol in most programming languages is the caret character (^).
Daniel Agrici
NovaCalculator Editorial Team
Our writers combine mathematical expertise with clear writing to make calculations accessible to everyone. Content is checked against authoritative sources including NIST, WHO, and CFPB.
Try the Calculator
XOR Calculator
Use the free tool to calculate instantly — no signup needed.