leetcode
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.
AC:
int getSum(int a, int b) {
int result;
while(b!=0)
{
result=a^b;
b=(a&b)<<1;
a=result;
}
return a;
}
tips: 简单易懂的解法,很受益。