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.
Frequently Asked Questions
Q: What is the difference between XOR and regular OR in everyday logic?
Regular OR (inclusive OR) returns true whenever at least one input is true β including the case where both inputs are true simultaneously. XOR (exclusive OR) is stricter: it returns true only when the inputs differ, meaning exactly one is true. The practical distinction matters in decision-making contexts. βYou can have cake or pieβ in everyday English usually means XOR β having both is not the intent. In contrast, a security system that triggers if sensor A OR sensor B fires uses inclusive OR, because both firing at once should still trigger the alarm. XOR models situations where the two conditions being simultaneously true is either impossible or should produce a different outcome.
Q: How is XOR used in cryptography and checksums?
XOR is central to both fields because of its self-inverse property: XOR-ing data with a key and then XOR-ing the result with the same key perfectly restores the original. In stream ciphers and one-time pads, each byte of plaintext is XOR-ed with a corresponding key byte to produce ciphertext; decryption is identical β XOR with the same key. In error detection, parity checking XOR-s all bits in a data block to produce a single check bit; if any bit flips in transit, re-computing the XOR reveals the mismatch. RAID 5 storage extends this idea across entire disk drives, using XOR parity to reconstruct data if one drive fails. Modern ciphers like ChaCha20 use XOR as a core mixing step within larger constructions.
Q: Can XOR be applied to more than two values at once?
Yes, and this is one of XORβs most useful properties. Because XOR is both associative and commutative, you can chain any number of values together and the result is the same regardless of evaluation order. XOR-ing a sequence of values produces a result that reflects the βodd-one-outβ parity across all of them: any value that appears an even number of times cancels itself out, leaving only the values that appear an odd number of times. This is why XOR-ing all elements of an array where every number appears twice except one immediately identifies the unique element β the pairs cancel to zero and only the lone value survives. The same principle drives multi-drive parity in RAID and multi-byte checksum calculations.
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.
Daniel Agrici
NovaCalculator Editorial Team
Our writers combine mathematical expertise with clear writing to make calculations accessible to everyone. Content is peer-reviewed for accuracy against authoritative sources including NIST, WHO, and CFPB.
Try the Calculator
XOR Calculator
Use the free tool to calculate instantly β no signup needed.