Binary XOR: How to Compare Bits and Hex Digits

Binary XOR and hexadecimal XOR examples showing aligned bits and four-bit nibbles

Binary XOR compares corresponding bits and returns 1 when the bits differ, or 0 when they match. To calculate it correctly, align both operands to the same width, apply the one-bit rule from left to right, and read the resulting bit string.

The same method works with hexadecimal values. Each hexadecimal digit represents a four-bit group, called a nibble, so hexadecimal XOR lets you process four aligned bits at a time.

The One-Bit Binary XOR Rule

The XOR, or exclusive OR, rule has four possible inputs:

  • 0 XOR 0 = 0
  • 0 XOR 1 = 1
  • 1 XOR 0 = 1
  • 1 XOR 1 = 0

In short, XOR produces 1 only when exactly one input bit is 1. Matching bits produce 0. This is a comparison operation, not ordinary addition: do not carry a 1 into the next position when both bits are 1.

How to Calculate XOR in Binary

Align operands to equal width

Write the operands in rows with their least significant bits, the rightmost bits, aligned. If one value has fewer bits, add leading zeros until both values have the same width. Leading zeros preserve the value while making each position comparable.

  1. Write both binary values at equal width.
  2. Compare the bits in each column.
  3. Write 1 for different bits and 0 for matching bits.
  4. Read the resulting row as the XOR result.

Worked example: 101101 XOR 011011

Both operands already contain six bits:

101101
011011

Compare each aligned position:

  • 1 XOR 0 = 1
  • 0 XOR 1 = 1
  • 1 XOR 1 = 0
  • 1 XOR 0 = 1
  • 0 XOR 1 = 1
  • 1 XOR 1 = 0

Therefore, 101101 XOR 011011 = 110110. Each output bit comes only from the two bits in the same column; there is no carry between columns.

How to Calculate XOR in Hexadecimal

Treat each hexadecimal digit as a four-bit nibble

Hexadecimal is shorthand for binary. The digits 0 through F represent four-bit patterns from 0000 through 1111. For example, 5 is 0101, A is 1010, 3 is 0011, and C is 1100.

To perform hexadecimal XOR, align the hexadecimal digits by position and XOR each pair of digits independently. You can expand each pair into four bits, apply the one-bit rule, then convert the four-bit result back to one hex digit. There is no carry between adjacent nibbles.

Worked example: 5A XOR 3C

Expand the two values into nibbles:

5A = 0101 1010
3C = 0011 1100

Process each nibble separately:

  • 5 XOR 3: 0101 XOR 0011 = 0110, which is 6.
  • A XOR C: 1010 XOR 1100 = 0110, which is 6.

Thus, 5A XOR 3C = 66. Hexadecimal XOR is the same bitwise operation as XOR in binary, written in a shorter form.

How to Check and Reverse an XOR Result

Apply the same mask twice to recover the original

XOR is reversible because applying the same value twice cancels its effect. For any value X and mask M:

(X XOR M) XOR M = X

Using the binary example, the result can be checked by applying the mask again:

110110 XOR 011011 = 101101

The original value returns because each bit follows this rule: a bit XORed with 0 stays unchanged, while a bit XORed with 1 flips once and flips back when XORed with 1 again.