XOR Calculator
Calculate the bitwise XOR of two numbers and see the result in binary, hex, octal and decimal.
Updated
XOR Calculator
Our XOR Calculator applies the exclusive OR operation to two
numbers bit by bit, the same as a ^ b in code. Type the numbers in decimal,
binary, octal or hex, choose a bit width from 1 to 64, and see the result in every format,
with A, B and the result lined up and the number of differing bits counted for you.
Quick answer
A bitwise XOR gives 1 where the two numbers’ bits are different and 0 where they are the same. For 12 (1100) and 10 (1010), the middle two columns differ, so 12 XOR 10 = 0110 = 6. Enter any two numbers above and the calculator shows the result instantly.
- Truth table: 0 ^ 1 and 1 ^ 0 give 1; 0 ^ 0 and 1 ^ 1 give 0
- In code: a ^ b (the caret is XOR, not a power: 2 ^ 3 = 1)
- Undoes itself: (a ^ b) ^ b = a, since a ^ a = 0 and a ^ 0 = a
- Hamming distance: the number of 1s in a ^ b is how many bits differ
What Is Bitwise XOR?
XOR, short for exclusive OR, is the logic operation that is true when exactly one of its two inputs is true. A bitwise XOR applies it to every bit position of two binary numbers at once, with no carrying between positions. It is written A ⊕ B in maths and a ^ b in most programming languages.
Another way to see it: XOR is addition without the carry. 1 + 1 = 10 in binary, and XOR keeps only the 0. That is exactly the sum bit of a half adder in a CPU, where the carry bit comes from A AND B.
XOR Truth Table
| A | B | A XOR B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Example Calculations
5 XOR 3: 0000 0101 ^ 0000 0011 = 0000 0110 = 6
2 XOR 3: 0000 0010 ^ 0000 0011 = 0000 0001 = 1
12 XOR 255: 0000 1100 ^ 1111 1111 = 1111 0011 = 243 (all bits flipped)
−8 XOR 255 (8-bit): 1111 1000 ^ 1111 1111 = 0000 0111 = 7
Notice that 12 XOR 10 and 5 XOR 3 both give 6: XOR only records where the bits differ, not what they were. Knowing the result and one of the inputs is enough to get the other back.
XOR Properties
| Property | Rule |
|---|---|
| Self-inverse | a ^ a = 0 |
| Identity | a ^ 0 = a |
| Undo | (a ^ b) ^ b = a |
| Flip all bits | a ^ (all ones) = NOT a |
| Order doesn’t matter | a ^ b = b ^ a, (a ^ b) ^ c = a ^ (b ^ c) |
Together these explain a classic puzzle: if every number in a list appears twice except one, XOR-ing them all leaves only the odd one out, because the pairs cancel. For example 3 ^ 5 ^ 3 = 5.
Uses of XOR
- Toggling bits: x ^ (1 << k) flips bit k; c ^ 32 switches an ASCII letter’s case (97 ^ 32 = 65, a to A).
- Encryption: XOR-ing data with a key hides it and XOR-ing again with the same key restores it (72 ^ 42 = 98, 98 ^ 42 = 72). Stream ciphers and the one-time pad are built on this.
- Error checking: parity bits, checksums and RAID 5 store the XOR of data blocks, so a lost block can be rebuilt by XOR-ing the parity with the blocks that survive.
- Comparing values: a ^ b is 0 only when a equals b, and counting its 1s gives the Hamming distance.
- Swapping without a temporary variable: a = a ^ b; b = a ^ b; a = a ^ b.
To compare XOR with AND, OR, NAND, NOR and XNOR on the same numbers, use the Bitwise Calculator.
Key Terms
- XOR
- Exclusive OR: 1 when exactly one input is 1.
- Hamming distance
- The number of bit positions in which two numbers differ.
- Mask
- A number whose 1 bits mark the positions an XOR should flip.
- Parity
- The XOR of a group of bits: 1 if the number of 1s is odd.
- Two’s complement
- The usual way to store negative numbers: −n in w bits is stored as 2w − n.
How to Use the XOR Calculator
- Enter A and B. Type the two numbers in the “Number A” and “Number B” boxes. Pressing Enter in A moves you to B. Decimal numbers can be negative.
- Pick the number format. Choose Dec, Bin, Oct or Hex for both numbers, or type a 0b, 0o or 0x prefix and the format switches by itself. Switching format converts the numbers already typed, so 12 becomes 1100 in binary.
- Set the bit width. Use the 8, 16, 32 or 64 chips, or type any width from 1 to 64. The width only matters when a negative number is involved.
- Read the result. The XOR appears as soon as both numbers are filled, in the same format you typed, with the working underneath (for example 12 XOR 10 = 6, 1100 ^ 1010 = 0110). Below it, A, B and the result are lined up bit by bit with the number of differing bits (the Hamming distance), and a table gives the result in binary, hexadecimal, octal, unsigned and signed decimal.
- Copy the result. Press Copy to copy the result without the spaces between digit groups. Press Clear or Esc to start again.
Example: enter A = 72 and B = 42 and the result is 98. Now enter A = 98 and B = 42, and the result is 72 again, the original value.
Who Can Use This Calculator?
Frequently Asked Questions (FAQs)
What is a bitwise XOR?
XOR (exclusive OR) compares two numbers one bit position at a time and gives 1 where the bits are different and 0 where they are the same. In most programming languages it is written with a caret: a ^ b. In maths it is often written A ⊕ B.
How do you calculate 12 XOR 10?
Write both numbers in binary: 12 = 1100 and 10 = 1010. Compare each column and write 1 where they differ: the 4s and 2s columns differ, the 8s and 1s columns match. So 12 XOR 10 = 0110 = 6.
What is the XOR truth table?
0 XOR 0 = 0, 0 XOR 1 = 1, 1 XOR 0 = 1 and 1 XOR 1 = 0. The output is 1 only when exactly one input is 1. That last row is what separates XOR from OR, where 1 OR 1 = 1.
Why is 2 ^ 3 equal to 1 in code?
In C, C++, Java, JavaScript, Python and most other languages, ^ means XOR, not “to the power of”. 2 = 10 and 3 = 11 in binary, which differ only in the last bit, so 2 ^ 3 = 01 = 1. For powers, use 2 ** 3 in Python and JavaScript, or Math.pow(2, 3) / pow(2, 3) in Java and C.
Why does XOR undo itself?
Because a ^ a = 0 and a ^ 0 = a, XOR-ing with the same value twice gives back what you started with: (a ^ b) ^ b = a. That is why XOR is used for simple ciphers (72 ^ 42 = 98, and 98 ^ 42 = 72 again), RAID parity and the XOR swap trick.
How is XOR used to find the Hamming distance?
The Hamming distance between two numbers is how many bit positions differ. A XOR B has a 1 exactly in those positions, so you count its 1s: 12 XOR 10 = 0110 has two 1s, so the Hamming distance is 2. The calculator shows this count under the bit diagram.
How do you flip or toggle bits with XOR?
XOR with a mask flips exactly the bits that are 1 in the mask and leaves the rest alone. x ^ (1 << k) toggles bit k, x ^ 0xFF flips all 8 low bits (12 ^ 255 = 243), and in ASCII a letter XOR 32 switches its case: 97 (a) ^ 32 = 65 (A).
How does XOR work with negative numbers?
Negative numbers are stored in two’s complement, so XOR works on that bit pattern. −1 has every bit set, so −1 XOR x = NOT x. In 8 bits, −8 is 1111 1000, and −8 XOR 255 = 0000 0111 = 7. The result’s sign bit is 1 only when exactly one of the two sign bits is 1.
Is the XOR Calculator free?
Yes. It is completely free, needs no sign-up, and can be used as many times as you like.
Try our XOR Calculator to find the bitwise XOR of any two numbers instantly.