What is inverse function to XOR?

JavaXorInverse

Java Problem Overview


There is XOR function in Java - a^b

For exemple: 5^3 = 6

Can you tell me inverse function? If I have 6 and 3 can i get range of numbers which include number 5?

Java Solutions


Solution 1 - Java

The inverse is XOR!

If you have:

c = a^b;

You can get a or b back if you have the other value available:

a = c^b; // or b^c (order is not important)
b = c^a; // or a^c

For example if a = 5, b = 3 (and thus c = 6 as you mentioned) you get:

b=0011 (3)            a=0101 (5)
c=0110 (6) XOR   or   c=0110 (6) XOR
----------            ----------
a=0101 (5)            b=0011 (3)

Solution 2 - Java

The inverse of XOR is XOR itself.
I came across a similar problem on leetcode. Leetcode-1720

Solution 3 - Java

The inverse of XOR is XOR itself. For example if you take this operation :

3 ^ 4 = 7

The following statements are true :

4 ^ 7 = 3

3 ^ 7 = 4

Hopefully, this helps.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionA.N.R.IView Question on Stackoverflow
Solution 1 - JavaVegerView Answer on Stackoverflow
Solution 2 - JavaNeel AlexView Answer on Stackoverflow
Solution 3 - JavaHimanshu GuptaView Answer on Stackoverflow