int Add(int x, int y)
{
if (y == 0)
return x;
else
return Add( x ^ y, (x & y) << 1);
}
Category: Programming
Swap two variable without any 3rd variable.
To swap two variables without using arithmetic operators, you can use the XOR operator.
There are 3 ways you can do this ,
#1
a ^= b ^= a ^= b;
#2
a = a ^ b;
b = a ^ b;
a = a ^ b;
#3
a = a + b –(b=a);