XOR calculator
Enter two numbers in binary, decimal or hex and get A XOR B, with the bits lined up so you can see which ones differ. Works with negative numbers and with hex strings of any length.
bits that differ → 1 · bits that match → 0
Fine-tune: bit width
A 0101B 0011XOR 0110
4-bit · 2 bits differ (set to 1)
Type 0x or 0b in front of a value to mix bases. Spaces and underscores are ignored.
How to calculate XOR
XOR (exclusive or) compares two numbers one bit at a time. Where the bits are different the answer has a 1, and where they are the same it has a 0. To do it by hand:
- Write both numbers in binary.
- Pad the shorter one with zeros on the left so the two line up.
- Go column by column: 1 if the two bits differ, 0 if they match.
- Convert the result back to decimal or hex if you need to.
That rule is the whole truth table:
| A | B | A XOR B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
The difference from ordinary OR is the last row. OR gives 1 when either bit is 1, including both. XOR gives 1 only when exactly one of them is, which is where the “exclusive” comes from. Flip every bit of the answer and you get XNOR, which marks the bits that match instead.
Worked example: 5 XOR 3 = 6
Step 1: Write both numbers in binary
Step 2: Pad them to the same length
Step 3: Compare each column
Step 4: Read the result
Result: 5 XOR 3 = 6. Two bits differ, so the answer has two 1s. The calculator above opens on this example.
XOR rules and a table for 0 to 7
A few rules cover most XOR puzzles and interview questions:
| Rule | Example |
|---|---|
| XOR with 0 changes nothing | 5 XOR 0 = 5 |
| XOR with itself gives 0 | 5 XOR 5 = 0 |
| XOR the same value twice and you are back where you started | (5 XOR 3) XOR 3 = 5 |
| Order does not matter | 5 XOR 3 = 3 XOR 5 |
| XOR with all 1s flips every bit | 0101 XOR 1111 = 1010 |
| The number of 1s in the result is how many bits differ | 5 XOR 3 = 110 → 2 |
For small numbers you can read the answer straight off this table. Find A down the side and B along the top:
| XOR | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 1 | 1 | 0 | 3 | 2 | 5 | 4 | 7 | 6 |
| 2 | 2 | 3 | 0 | 1 | 6 | 7 | 4 | 5 |
| 3 | 3 | 2 | 1 | 0 | 7 | 6 | 5 | 4 |
| 4 | 4 | 5 | 6 | 7 | 0 | 1 | 2 | 3 |
| 5 | 5 | 4 | 7 | 6 | 1 | 0 | 3 | 2 |
| 6 | 6 | 7 | 4 | 5 | 2 | 3 | 0 | 1 |
| 7 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
The 0s run down the diagonal (every number XOR itself), and the 0 row and column just repeat the other number.
XOR in Python, JavaScript, C and Excel
Most programming languages write XOR as ^. Excel has a function for it instead:
5 ^ 3 # Python: 6
5 ^ 3 // JavaScript, C, C++, Java, C#, Go, Rust: 6
0x1F ^ 0xFF // hex works the same: 0xE0 (224)
=BITXOR(5, 3) ' Excel and Google Sheets: 6Three mistakes come up again and again:
- ^ is not “to the power of”. In code,
2 ^ 3is 1, not 8. Use2 ** 3in Python and JavaScript, orpow(2, 3)in C. - JavaScript cuts numbers to 32 bits.
2 ** 32 ^ 1gives 1, because the high bit is dropped first. Use BigInt for big values:2n ** 32n ^ 1ngives 4294967297n. - Excel’s BITXOR has limits. It returns
#NUM!for negative numbers, decimals, and anything above 248 − 1.
Negative numbers work in code because they are stored in two’s complement, so -5 ^ 3 is −8 in Python, JavaScript and C. Type a minus sign in the calculator and it switches to the same signed view.
What XOR is used for
- Flipping chosen bits.
flags ^ 0b100turns the third bit on if it was off and off if it was on, leaving the rest alone. - Simple encryption. XOR the data with a key to scramble it, and XOR with the same key again to get it back. With a truly random key as long as the message, this is the one-time pad. With a short repeating key, it is easy to break.
- Parity and RAID. RAID 5 stores the XOR of the data blocks. If one drive fails, XOR of everything that is left rebuilds it.
- Comparing values.
a ^ bis 0 only when the two are identical, and the 1s show exactly which bits differ (the Hamming distance). - The “find the odd one out” puzzle. XOR every number in a list where all but one appear twice. The pairs cancel to 0 and the one left over is the answer.
In puzzles and security exercises you often XOR two hex strings of the same length. Paste both into the calculator in Hex mode; there is no length limit. The second example shows one from the well-known Cryptopals exercises.
Frequently asked questions
What is 1 XOR 1?
0. XOR gives 1 only when the two bits are different, so 1 XOR 1 and 0 XOR 0 are both 0, while 1 XOR 0 and 0 XOR 1 are both 1.
What is the difference between XOR and OR?
Only the case where both bits are 1. OR gives 1 there, XOR gives 0. So 5 OR 3 is 7 (111), but 5 XOR 3 is 6 (110).
What is the symbol for XOR?
In maths and logic it is ⊕ (A ⊕ B). Most programming languages use ^, some languages and calculators write XOR, and ARM processors call the instruction EOR.
What is the opposite of XOR?
XNOR. It gives 1 where the bits match and 0 where they differ, which is XOR with every bit flipped. Use the XNOR calculator for that.
Sources
- MDN: Bitwise XOR (^): 32-bit behaviour and BigInt
- Python docs: binary bitwise operations: the ^ operator on integers
- Microsoft Support: BITXOR function: limits and #NUM! errors
- Cryptopals: Fixed XOR: the hex string example
Related Tools
Build and preview Valorant crosshair settings.
Search common Vim commands.
Turn JSON rows into bar, line, or pie charts.
Convert WebP files and Base64 data in both directions.
Convert GIF files and Base64 data in both directions.
Save any GIF frame, or all frames, as JPG.