How XOR (Exclusive OR) Works
XOR, short for "exclusive OR," is one of the four basic bitwise logic operations (alongside AND, OR, and NOT). It compares two numbers one binary digit at a time and outputs a 1 wherever the two bits are different, and a 0 wherever they are the same. This calculator converts your two decimal inputs to binary, XORs them bit by bit, and shows the result in decimal, binary, and hexadecimal, along with how many bit positions differ between the two inputs.
The XOR truth table and formula
For a single pair of bits, XOR follows this table: 0⊕0 = 0, 0⊕1 = 1, 1⊕0 = 1, 1⊕1 = 0. To XOR two full numbers, write both in binary with the same number of digits (pad with leading zeros), line them up by place value, and apply the truth table to each column independently — there is no carrying between columns, unlike addition. For example, 12 XOR 10 in 8-bit binary: 00001100 ⊕ 00001010 = 00000110, which is 6 in decimal. Algebraically, XOR is equivalent to addition modulo 2 in each bit position, and it can also be expressed as (A OR B) AND NOT (A AND B) — true wherever at least one input is true but not both.
Bit width and range
Because computers store numbers using a fixed number of bits, this calculator lets you choose a bit width (4, 8, 16, or 32 bits) that determines the padding of the binary output and the maximum value each input can hold (2width − 1). Choosing a width that is too small for your numbers will trigger a validation error; choosing a wider one than necessary is always safe and simply adds leading zeros.
Common sources of error
- Confusing XOR with OR: OR returns 1 when either or both bits are 1; XOR returns 1 only when the bits are different, so 1⊕1 = 0 while 1 OR 1 = 1.
- Forgetting to pad with zeros: when comparing binary numbers of different lengths by hand, misaligning the columns gives the wrong answer — always pad the shorter number with leading zeros first.
- Using negative numbers: this calculator works with non-negative integers; negative numbers require a defined bit width and two's-complement convention to XOR correctly.
Real-world applications
- Error detection: parity bits and checksums (like CRC) use repeated XOR operations to detect single-bit transmission errors.
- Simple encryption: XOR ciphers and one-time pads encrypt data by XOR-ing it with a key, and decrypt it by XOR-ing again with the same key, since A⊕B⊕B = A.
- Swapping variables: the classic "XOR swap" exchanges two variables without a temporary storage variable: a = a⊕b; b = a⊕b; a = a⊕b.
- Toggling bits and graphics: XOR is used to flip specific bits (a flag register, a pixel mask) while leaving all other bits unchanged.