Calculate the sum of two integers a and b, but you are not allowed to use the operator +
and -
.
計算兩個整數a,b之和,不能使用操作符+或-。
Example:
Given a = 1 and b = 2, return 3.
int getSum(int a, int b) {
int sum,carry=0;
sum=a^b;
carry=(a&b)<<1;
if(carry==0)
return sum;
else
return getSum(sum,carry);
}