题目描述:
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.
思想:(1)两个数异或,相当于两个数的二进制不带进位相加
(2)两个数相与,相当于得到要进位的数字
(3)循环相加,直到没有进位
public int getSum(int a, int b) {
int tempSum = 0;
do{
tempSum = a ^ b;
int c = (a & b) << 1;
a = tempSum;
b = c;
} while(b != 0);
return a;
}