If I XOR 2 numbers, I get only 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 ?

+4
source share
4 answers

Yes.

z = y because x ^ y ^ x = y

Thus, the combination a ^ b = y = z is quite possible.

Indeed, for every a there exists a b such that a ^ b = z . To calculate this, b = z ^ a .

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

+5
source

Short answer: Yes

Long answer: XOR is a binary operation, it works with individual bits and is commutative.

He has a truth table:

 ABQ 0 0 0 0 1 1 1 0 1 1 1 0 

Since the number is composed of these bits, the result will be the same if both bits have the same result for each bit position. For example, take 2 8-bit numbers 113 and 42

 113 = 01110001 42 = 00101010 XOR = 01011011 = 91 

but if I change the fourth bit on the left, I get

 97 = 01100001 58 = 00111010 XOR = 01011011 = 91 

So yes again ...

+8
source

Yes. As a degenerate proof, an XORing number with itself always leads to 0.

+4
source

XOR will return true if both parameters are different, assuming that in any case the parameters are boolean values. This is different from or, which will return true if either parameter is true, and NOR, which will return true only if both are false.

0
source

Source: https://habr.com/ru/post/1304631/


All Articles