371.
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)
{
if (b == 0)
return a;
else {
int sum = a^b;
int temp = (a&b) << 1;
return (getSum(sum, temp));
}
}
};