一、问题描述
Calculate the sum of two integers a and b,
but you are not allowed to
use the operator +
and -
.
二、解决思路
两个数的相加用异或,进位用与。
三、代码
class Solution {
public:
int getSum(int a, int b) {
while(b){
int c = a & b;
a = a ^ b;
b = c << 1;
}
return a;
}
};