What Is the XOR Operation?
XOR (eXclusive OR) is a fundamental bitwise logical operation that compares two binary digits (bits) and returns 1 when the inputs differ, and 0 when they are the same. In plain English: "one or the other, but not both."
The Boolean expression for XOR is written as A ⊕ B = Y, also seen as A ^ B in most programming languages.
The rule is simple: same inputs → 0; different inputs → 1.
Exclusive OR Truth Table
The truth table below defines the XOR output for every possible pair of single-bit inputs A and B:
| A | B | A ⊕ B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
The XOR Gate
In digital electronics, the XOR gate is a logic gate built from a combination of AND, OR, and NOT gates. Its output is HIGH (1) only when an odd number of its inputs are HIGH. For a standard two-input gate, this means exactly one input is HIGH. When both inputs share the same state (both 0 or both 1), the output is LOW (0).
XOR gates are widely used in adder circuits, comparators, parity checkers, and error-detection hardware.
How to Calculate the XOR of Two Numbers
To XOR two integers, convert both to binary and apply the XOR rule to each pair of corresponding bits:
- Write both numbers in binary (pad with leading zeros to the same length).
- For each bit position, apply the truth table: same bits → 0, different bits → 1.
- The resulting binary number is the XOR output.
- Convert back to decimal (or any other base) as needed.
Example: 10 XOR 7
10 in binary: 0000 1010 7 in binary: 0000 0111 XOR: 0000 1101 = 13 (decimal)
Bit-by-bit: bits 0–2 differ → output 1; bit 3 matches 1,1 → 0; bit 3 from 10 is 1 and from 7 is 0 → 1, etc. The final result is 13.
How to Use the XOR Calculator
- Select the Input Format: Decimal (base 10), Binary (base 2), or Octal (base 8).
- Enter Number A in the chosen format (non-negative integer).
- Enter Number B in the same format.
- Click Calculate XOR.
- The results panel shows the XOR output in Decimal, Binary, Octal, and Hexadecimal, plus a bit-by-bit analysis table for numbers up to 32 bits wide.
Applications of XOR Logic
- Cryptography & encryption — XOR cipher (Vernam / one-time pad), stream ciphers, and block-cipher round functions all rely on XOR.
- Error detection & correction — parity bits and CRC checksums use XOR to detect single-bit (or multi-bit) transmission errors.
- In-place swap without a temporary variable —
a ^= b; b ^= a; a ^= b;swaps two integers using only XOR. - Hashing & checksums — fast XOR-based hash functions combine bytes or words of data.
- RAID-5 parity — XOR parity across disk stripes lets RAID-5 reconstruct a failed drive.
- Gray code generation — adjacent Gray-code values differ by exactly one bit, derived via XOR.
- Pseudo-random number generators — XOR-shift PRNGs use repeated XOR and shift operations.
- Toggle bits in microcontroller registers — XOR with a mask flips specific bits without a read-modify-write cycle.
Frequently Asked Questions (FAQ)
Is XOR the same as OR?
No. OR returns 1 when at least one input is 1 (including both). XOR returns 1 only when inputs are different. When both inputs are 1, OR gives 1 but XOR gives 0.
What is XOR of a number with itself?
A ⊕ A = 0 for any integer A, because every bit cancels itself out. This property is used to reset registers and detect duplicate values.
What is XOR of a number with zero?
A ⊕ 0 = A for any integer A, because XOR with 0 leaves every bit unchanged. This is the identity property of XOR.
Is XOR commutative and associative?
Yes to both. A ⊕ B = B ⊕ A (commutative) and (A ⊕ B) ⊕ C = A ⊕ (B ⊕ C) (associative), which makes XOR useful in flexible reductions over sequences.
How does XOR relate to addition in binary?
XOR is the sum without carry in binary addition. The carry is produced by AND. This is why XOR is the core of a half-adder circuit.
Can XOR be used for negative numbers?
In most languages and processors, XOR operates on the two's-complement bit pattern of integers, including negative values. This calculator works with non-negative integers only; for signed XOR, the result depends on the chosen bit width.