Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If I XOR 2 numbers, do I only get identical results if the numbers are the same?

For example, suppose I have x XOR y = y XOR x = z. Is it possible to have something like a XOR b = z?

like image 631
they changed my name Avatar asked Dec 02 '22 06:12

they changed my name


1 Answers

Short answer: Yes

Long answer: XOR is a binary operation, it works on the individual bits and it's commutative.

It has the truth table:

A B  Q
0 0  0
0 1  1
1 0  1
1 1  0

As the number is made up of these bits then the result will be the same as long as for each bit position the two bits have the same result. For example take the 2 eight bit numbers 113 and 42

113 = 01110001
42  = 00101010
XOR = 01011011 = 91

but if I swap the fourth bit from the left I get

97  = 01100001
58  = 00111010
XOR = 01011011 = 91

So yes again...

like image 65
James Avatar answered Dec 05 '22 01:12

James