Find XOR of two number without using XOR operator

Given two integers, find XOR of them without using XOR operator, i.e., without using ^ in C/C++.

Examples :

```Input:  x = 1, y = 2
Output: 3

Input:  x = 3, y = 5
Output: 6

```

A Simple Solution is to traverse all bits one by one. For every pair of bits, check if both are same, set the corresponding bit as 0 in output, otherwise set as 1.

 `// C++ program to find XOR without using ^` `#include ` `using` `namespace` `std;`   `// Returns XOR of x and y` `int` `myXOR(``int` `x, ``int` `y)` `{` `    ``int` `res = 0; ``// Initialize result` `    `  `    ``// Assuming 32-bit Integer ` `    ``for` `(``int` `i = 31; i >= 0; i--)                     ` `    ``{` `       ``// Find current bits in x and y` `       ``bool` `b1 = x & (1 << i);` `       ``bool` `b2 = y & (1 << i);` `       `  `        ``// If both are 1 then 0 else xor is same as OR` `        ``bool` `xoredBit = (b1 & b2) ? 0 : (b1 | b2);          `   `        ``// Update result` `        ``res <<= 1;` `        ``res |= xoredBit;` `    ``}` `    ``return` `res;` `}`   `// Driver program to test above function` `int` `main()` `{` `   ``int` `x = 3, y = 5;` `   ``cout << ``"XOR is "` `<< myXOR(x, y);` `   ``return` `0; ` `}`

 `// Java program to find XOR without using ^` `import` `java.io.*;`   `class` `GFG{` `  `  `// Returns XOR of x and y` `static` `int` `myXOR(``int` `x, ``int` `y)` `{` `    `  `    ``// Initialize result` `    ``int` `res = ``0``; `   `    ``// Assuming 32-bit Integer ` `    ``for``(``int` `i = ``31``; i >= ``0``; i--)                     ` `    ``{` `        `  `        ``// Find current bits in x and y` `        ``int` `b1 = ((x & (``1` `<< i)) == ``0` `) ? ``0` `: ``1``;  ` `        ``int` `b2 = ((y & (``1` `<< i)) == ``0` `) ? ``0` `: ``1``;  `   `        ``// If both are 1 then 0 else xor is same as OR` `        ``int` `xoredBit = ((b1 & b2) != ``0``) ? ``0` `: (b1 | b2);          `   `        ``// Update result` `        ``res <<= ``1``;` `        ``res |= xoredBit;` `    ``}` `    ``return` `res;` `}`   `// Driver Code` `public` `static` `void` `main (String[] args)` `{` `    ``int` `x = ``3``, y = ``5``;` `    `  `    ``System.out.println(``"XOR is "` `+ myXOR(x, y));` `}` `}`   `// This code is contributed by math_lover`

 `# Python3 program to find XOR without using ^`   `# Returns XOR of x and y` `def` `myXOR(x, y):` `    ``res ``=` `0` `# Initialize result`   `    ``# Assuming 32-bit Integer` `    ``for` `i ``in` `range``(``31``, ``-``1``, ``-``1``):` `        `  `        ``# Find current bits in x and y` `        ``b1 ``=` `x & (``1` `<< i)` `        ``b2 ``=` `y & (``1` `<< i)` `        ``b1 ``=` `min``(b1, ``1``)` `        ``b2 ``=` `min``(b2, ``1``)`   `        ``# If both are 1 then 0 ` `        ``# else xor is same as OR` `        ``xoredBit ``=` `0` `        ``if` `(b1 & b2):` `            ``xoredBit ``=` `0` `        ``else``:` `            ``xoredBit ``=` `(b1 | b2)`   `        ``# Update result` `        ``res <<``=` `1``;` `        ``res |``=` `xoredBit` `    ``return` `res`   `# Driver Code` `x ``=` `3` `y ``=` `5` `print``(``"XOR is"``, myXOR(x, y))`   `# This code is contributed by Mohit Kumar`

Output :

```XOR is 6

```

Thanks to Utkarsh Trivedi for suggesting this solution.

A Better Solution can find XOR without using loop.
1) Find bitwise OR of x and y (Result has set bits where either x has set or y has set bit). OR of x = 3 (011) and y = 5 (101) is 7 (111)
2) To remove extra set bits find places where both x and y have set bits. The value of expression “~x | ~y” has 0 bits wherever x and y both have set bits.
3) bitwise AND of “(x | y)” and “~x | ~y” produces the required result.

Below is the implementation.

 `// C++ program to find XOR without using ^` `#include ` `using` `namespace` `std;`   `// Returns XOR of x and y` `int` `myXOR(``int` `x, ``int` `y)` `{` `   ``return` `(x | y) & (~x | ~y);` `}`   `// Driver program to test above function` `int` `main()` `{` `   ``int` `x = 3, y = 5;` `   ``cout << ``"XOR is "` `<< myXOR(x, y);` `   ``return` `0; ` `}`

 `// Java program to find` `// XOR without using ^` `import` `java.io.*;`   `class` `GFG ` `{`   `// Returns XOR of x and y` `static` `int` `myXOR(``int` `x, ``int` `y)` `{` `    ``return` `(x | y) & ` `           ``(~x | ~y);` `}`   `// Driver Code` `public` `static` `void` `main (String[] args) ` `{` `    ``int` `x = ``3``, y = ``5``;` `    ``System.out.println(``"XOR is "``+ ` `                      ``(myXOR(x, y)));` `}` `}`   `// This code is contibuted by ajit`

 `# Python 3 program to ` `# find XOR without using ^`   `# Returns XOR of x and y` `def` `myXOR(x, y):` `    ``return` `((x | y) & ` `            ``(~x | ~y))`   `# Driver Code` `x ``=` `3` `y ``=` `5` `print``(``"XOR is"` `, ` `       ``myXOR(x, y))`   `# This code is contributed` `# by Smitha`

 `// C# program to find` `// XOR without using ^` `using` `System;`   `class` `GFG` `{` `    `  `// Returns XOR of x and y` `static` `int` `myXOR(``int` `x, ``int` `y)` `{` `    ``return` `(x | y) & ` `           ``(~x | ~y);` `}`   `// Driver Code` `static` `public` `void` `Main ()` `{` `    ``int` `x = 3, y = 5;` `    ``Console.WriteLine(``"XOR is "``+ ` `                     ``(myXOR(x, y)));` `}` `}`   `// This code is contibuted by m_kit`

 ``

Output :

```XOR is 6

```

Thanks to jitu_the_best for suggesting this solution.

Alternate Solution :

 `// C++ program to find XOR without using ^` `#include ` `using` `namespace` `std;`   `// Returns XOR of x and y` `int` `myXOR(``int` `x, ``int` `y)` `{` `   ``return` `(x & (~y)) | ((~x )& y);` `}`   `// Driver program to test above function` `int` `main()` `{` `   ``int` `x = 3, y = 5;` `   ``cout << ``"XOR is "` `<< myXOR(x, y);` `   ``return` `0; ` `}`

 `// Java program to find XOR without using ^` `import` `java.io.*;` ` `  `class` `GFG ` `{`   `// Returns XOR of x and y` `static` `int` `myXOR(``int` `x, ``int` `y)` `{` `return` `(x & (~y)) | ((~x )& y);` `}`   `// Driver Code` `public` `static` `void` `main (String[] args) ` `{` ` `  `int` `x = ``3``, y = ``5``;` `System.out.println(``"XOR is "``+ ` `                      ``(myXOR(x, y)));` `} ` `}`   `// This code is contributed by shivanisinghss2110`

 `# Python3 program to ` `# Returns XOR of x and y` `def` `myXOR(x, y):` `    ``return` `(x & (~y)) | ((~x )& y)`   `# Driver Code` `x ``=` `3` `y ``=` `5` `print``(``"XOR is"` `, ` `    ``myXOR(x, y))`   `# This code is contributed by shivanisinghss2110`

 `// C# program to find XOR without using ^` `using` `System;`   `class` `GFG{`   `// Returns XOR of x and y` `static` `int` `myXOR(``int` `x, ``int` `y)` `{` `    ``return` `(x & (~y)) | ((~x )& y);` `}`   `// Driver program to test above function` `public` `static` `void` `Main()` `{` `    ``int` `x = 3, y = 5;` `    ``Console.WriteLine(``"XOR is "` `+myXOR(x, y));` `} ` `}`   `// This code is contributed by shivansinghss2110`