18

It's my understanding that when you XOR something, the result is the sum of the two numbers mod $2$.

Why then does $4 \oplus 2 = 6$ and not $0$? $4+2=6$, $6%2$ doesn't equal $6$. I must be missing something about what "addition modulo 2" means, but what?

100 // 4

010 // XOR against 2

110 = 6 // why not zero if xor = sum mod 2?

Thinh D. Nguyen
  • 2,275
  • 3
  • 23
  • 71
snerd
  • 394
  • 1
  • 2
  • 9

4 Answers4

26

The confusion here stems from a missing word. A correct statement is "The result of XORing two bits is the same as that of adding those two bits mod 2."

For example, $(0+1)\bmod 2 = 1\bmod 2 = 1=(0\text{ XOR }1)$

and

$(1+1) \bmod 2= 2\bmod 2 = 0 =(1\text{ XOR }1)$

socket_var
  • 103
  • 2
Rick Decker
  • 14,826
  • 5
  • 42
  • 54
4

The xor of two one-bit numbers is their sum modulo 2. But the xor of two $n$-bit numbers can't possibly be their sum modulo 2: any value modulo 2 is either zero or one but the xor of two $n$-bit numbers could be anything between 0 and $2^n-1$, inclusive.

David Richerby
  • 81,689
  • 26
  • 141
  • 235
4

You are confusing operations on a single bit with operations on a byte,or word.(Multiple bits) A single bit represents either 0 or 1 depending on its value. If you add two bits, and ignore the carry, you are adding "mod2". 0+0 = 0 0+1 = 1 1+0 = 1 1+1 overflows, or carries, and you have 0

this is exactly the same as XOR

However it is NOT true for words. Consider a nibble (4 bits) 0110 + 0111 = 6 + 7 = 13 = 1101 0110 xor 0111 = 0001 = 1

Michael M
  • 49
  • 1
1

I think you're misunderstanding the property.

I believe that property is more concerned with the boolean result of the XOR function, not the numeric value. What I mean is that each "number" you're adding is actually just a boolean value of True (1) or False (0) and adding those together, mod 2, is the same as XOR of those values.

This page may clear that up further: The Magic of XOR

Trass3r
  • 103
  • 2
wateryan
  • 11
  • 3