How to use the Bitwise XOR Calculator tool
- Enter the first binary number in Input A. The Example button uses 1100.
- Enter the second binary number, or a key or toggle mask, in Input B. The Example button uses 1010.
- Select Calculate to XOR the two values.
- Read the output in the Result box. For the sample values it is 0110.
- Select Copy to copy the result. To undo the XOR, enter it as Input A and calculate with the same Input B.
How exclusive OR works
XOR, short for exclusive OR, outputs 1 when exactly one of the two input bits is 1. Two zeros give 0 and two ones also give 0. Another way to read it: the result bit says whether the inputs differ in that column. It is also addition modulo 2, a sum with the carry thrown away.
XOR with 0 leaves a bit alone, and XOR with 1 flips it. A mask therefore selects which bits to toggle.
Bitwise XOR example and its reversibility
XOR is its own inverse. Apply the same second operand twice and you are back where you started. The example below XORs the byte 01001000 (ASCII H) with the key 00101010, then XORs the output with the key again.
This is the basis of stream ciphers and the one-time pad: the sender XORs the message with a keystream, and the receiver XORs with the same keystream to recover it.
01001000 XOR 00101010 = 01100010
01100010 XOR 00101010 = 01001000Input rules and unequal lengths
Input A and Input B each accept 1 to 65,536 binary digits. A leading 0b and any spaces are removed. The shorter value is padded with zeros on the left, and since XOR with 0 changes nothing, the extra high bits of the longer value are copied into the result: 101 XOR 11000 is 11101.
The output always has the width of the longer input. Equal inputs return a row of zeros, for example 1011 XOR 1011 is 0000, which is a quick way to confirm two bit strings are identical.
Where developers use XOR
Few operations turn up in as many different areas as XOR does.
- Parity and checksums: XORing all bytes of a message gives a simple longitudinal redundancy check.
- RAID 5: the parity block is the XOR of the data blocks, so any one lost block can be rebuilt from the others.
- Toggling state: ASCII A (01000001) XOR 00100000 gives a (01100001), and the same mask flips it back.
- Finding differences: the ones in A XOR B are the bit positions that changed, and counting them gives the Hamming distance.
- Hash mixing, CRC arithmetic and Gray code conversion, which all rely on carry-free addition.
Frequently asked questions
What is 1100 XOR 1010?
It equals 0110. The two middle columns contain different bits, so they become 1. The outer columns contain equal bits and become 0. In decimal, 12 XOR 10 is 6.
What is the difference between XOR and OR?
They disagree only when both bits are 1. OR returns 1 in that case and XOR returns 0. For 1100 and 1010, OR gives 1110 and XOR gives 0110.
Is the caret operator in my language XOR or exponentiation?
In C, C++, Java, JavaScript, Python, Go and Rust the caret is bitwise XOR, so 2 ^ 3 is 1, not 8. Exponentiation uses a function such as pow or a separate operator such as two asterisks.
Why does XORing a value with itself give zero?
Every bit is compared with an identical bit, so no column differs and every output bit is 0. Assembly programmers use this to clear a register, as in xor eax, eax on x86.
Can I XOR more than two numbers with this tool?
The form takes two inputs at a time. XOR is associative and commutative, so calculate the first pair, enter the result as Input A, put the next value in Input B and repeat. The order does not affect the final answer.
Is XOR encryption secure?
Only when the key is truly random, at least as long as the message and never reused, which is the one-time pad. XOR with a short or repeated key is easy to break by frequency analysis.