Program to find xor of two numbers in python

Given two integers, find XOR of them without using the 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 the same, set the corresponding bit like 0 in output, otherwise set it as 1. 

C++

#include

using namespace std;

int myXOR[int x, int y]

{

    int res = 0;

    for [int i = 31; i >= 0; i--]                    

    {

       bool b1 = x & [1

Javascript

function myXOR[x, y]

{

   return [x | y] & [~x | ~y];

}

   let x = 3, y = 5;

   document.write["XOR is " + myXOR[x, y]];

Time Complexity: O[1]

Space Complexity: O[1]

Thanks to jitu_the_best for suggesting this solution. 

Alternate Solution : 

C++

#include

using namespace std;

int myXOR[int x, int y]

{

   return [x & [~y]] | [[~x ]& y];

}

int main[]

{

   int x = 3, y = 5;

   cout

Chủ Đề