Skip to content

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 0101
B 0011
XOR 0110
Decimal
6
Binary
110
Hex
6

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:

  1. Write both numbers in binary.
  2. Pad the shorter one with zeros on the left so the two line up.
  3. Go column by column: 1 if the two bits differ, 0 if they match.
  4. Convert the result back to decimal or hex if you need to.

That rule is the whole truth table:

XOR truth table
ABA XOR B
000
011
101
110

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

5 = 101  ·  3 = 11

Step 2: Pad them to the same length

5 = 101 3 = 011

Step 3: Compare each column

1 vs 0 → 1  ·  0 vs 1 → 1  ·  1 vs 1 → 0

Step 4: Read the result

110 = 4 + 2 = 6

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:

Rules of XOR with examples
RuleExample
XOR with 0 changes nothing5 XOR 0 = 5
XOR with itself gives 05 XOR 5 = 0
XOR the same value twice and you are back where you started(5 XOR 3) XOR 3 = 5
Order does not matter5 XOR 3 = 3 XOR 5
XOR with all 1s flips every bit0101 XOR 1111 = 1010
The number of 1s in the result is how many bits differ5 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 of every pair of numbers from 0 to 7
XOR01234567
001234567
110325476
223016745
332107654
445670123
554761032
667452301
776543210

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: 6

Three mistakes come up again and again:

  • ^ is not “to the power of”. In code, 2 ^ 3 is 1, not 8. Use 2 ** 3 in Python and JavaScript, or pow(2, 3) in C.
  • JavaScript cuts numbers to 32 bits. 2 ** 32 ^ 1 gives 1, because the high bit is dropped first. Use BigInt for big values: 2n ** 32n ^ 1n gives 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 ^ 0b100 turns 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 ^ b is 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