they changed my name
they changed my name

Reputation: 491

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?

Upvotes: 5

Views: 11050

Answers (4)

James
James

Reputation: 1041

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...

Upvotes: 10

PP.
PP.

Reputation: 10864

Yes.

z = y because x ^ y ^ x = y

So it is entirely possible for a combination a ^ b = y = z.

In fact, for every a there exists a b such that a ^ b = z. To calculate that, b = z ^ a.

Be aware that XOR is commutative: this means that x ^ y = y ^ x.

Upvotes: 5

Leif Andersen
Leif Andersen

Reputation: 22352

XOR, will return true if both parameters are different, assuming that the parameters are Boolean values anyway. This is different from or, which will return true if either parameter is true, and NOR, which will return true only if both of them are false.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799170

Yes. As a degenerate proof, XORing a number with itself always results in 0.

Upvotes: 4

Related Questions