Microsoft Interview Question

Swap two nos without using third variable as well as arithmetic operator. It was a cake walk. It can be done using bitwise xor(^) operator.

Interview Answer

Anonymous

Jul 26, 2018

The bitwise XOR operator can be used to swap two variables. The XOR of two numbers x and y returns a number which has all the bits as 1 wherever bits of x and y differ. For example XOR of 10 (In Binary 1010) and 5 (In Binary 0101) is 1111 and XOR of 7 (0111) and 5 (0101) is (0010).