How to XOR numbers together then extract a number

1.2k views Asked by At

Let's say a, b, c are three integers. Then XOR them together and get d = a ^ b ^ c.

If I only know "a" and "d", and there is no relationship between a, b, c. Is there a way to check if "a" has been used to produce "d"?

For example:

d = a ^ b ^ c;

func(a, d); //returns true

func(b, d); //returns true

func(f, d); //returns false, for f!=a, f!=b, f!=c

I mean "d" is the result of "a^b^c", and we don't know that in advance. Now given "d" and a number "x", can I check if x is one of a or b or c?

Sorry I forget to mention that a, b, c, d are NOT boolean variable here. I use them to stand for integers in any programming language. So d = a^b^c is a bitwise operation. For example, d equals to the result of 2^18^19.

2

There are 2 answers

4
ruakh On

No, because for any a and d, you can make the equation true, e.g. by setting b = 0 and c = a ^ d.

For example, let's take your example, where d = 2^18^19. You want func(12, d) to be 'false', because 12 is not equal to 2 or 18 or 19. The problem is that d is just 3; it doesn't "remember" having been made from 2 and 8 and 19 at all. So d = 2^18^19 = 3 = 12^0^15 = 7^12^8 = 4^11^12 = …, and there's no reason, given just d = 3, to view 12 or 0 or 15 or 7 or 8 or 4 or 11 as being any different from 2 or 18 or 19.

0
Luca Angioloni On

No you cannot: for any couple of numbers (a and d) you can always find another number (b) that XORed with one of the numbers in the couple gives the other.

d = a ^ b or a = d ^ b

Moreover you are trying to do it with multiple XOR...

XOR is in fact used as a primitive method of encryption. You could try and guess that if you had a lot of numbers d created through XOR searching for a pattern.