Given two integers, find their XOR without using the XOR operator.

Practice this problem

A naive solution would be to consider every bit present in both numbers one by one (either from left or right) and compare them. If the current bit is the same in both numbers (i.e., both are 0 or both are 1), set it as 0; otherwise, set it as 1 and move to the next pair of bits until all bits are processed.

 
The expression (x | y) - (x & y) is equivalent to x ^ y (finding XOR of two numbers x and y). The XOR works by setting the bits which are set in either of one of the given numbers (0 ^ 1 = 1, 1 ^ 0 = 1) and finally taking out the common bits present in both numbers (1 ^ 1 = 0).

 
For example,

01000001    |               (x = 65)
01010000                    (y = 80)
~~~~~~~~
01010001                    (x | y)
 
 
01000001    &               (x = 65)
01010000                    (y = 80)
~~~~~~~~
01000000                    (x & y)
 

Now, the result x ^ y would be (x | y) - (x & y) = (01010001 - 01000000) = 00010001. Following is the C++, Java, and Python program that demonstrates it:

C++


Download  Run Code

Output:

The first number in binary is 01000001
The second number in binary is 01010000
 
XOR is 00010001

Java


Download  Run Code

Output:

The first number in binary is 1010001
The second number in binary is 1000000
 
XOR is 10001

Python


Download  Run Code

Output:

The first number in binary is 0b1010001
The second number in binary is 0b1000000
 
XOR is 0b10001

Suggested Read: https://graphics.stanford.edu/~seander/bithacks.html