Calculate the sum of two integers a and b, but you are not allowed to use the operator +
and -
.
Example:
Given a = 1 and b = 2, return 3.
int getSum(int a, int b) {
int sum = a;
while(b){//如果存在进位,则不断的亦或求和
sum = a^b;//亦或,算出不带进位的和
b = (a&b)<<1;//算出进位的值
a = sum;
}
return sum;
}