class Solution{
public:
int Add(int num1,int num2)
{
int res1,res2;
int sum;
res1 = num1 ^ num2;
res2 = num1 & num2;
while(res2)
{
num1 = res1;
num2 = res2 << 1;
res1 = num1 ^ num2;
res2 = num1 & num2;
}
return res1;
}
};
代码为c++
1. 位运算加法 a+b = a^b + (a&b)<< 1
2. &: 与运算
有0为0,无0为1
3. |: 或运算
有1为1,无1为0
4. ^: 异或运算
相同为0,不同为1
5. ~: 取反运算
1为0,0为1
6. <<:左移
7. >>:右移
参考来源:http://blog.csdn.net/zhongjiekangping/article/details/6855864