给出两个整数a和b, 求他们的和, 但不能使用 +
等数学运算符。
注意事项
你不需要从输入流读入数据,只需要根据aplusb
的两个参数a和b,计算他们的和并返回就行。
说明
a和b都是 32位
整数么?
- 是的
我可以使用位运算符么?
- 当然可以
样例
如果 a=1
并且 b=2
,返回3
挑战
显然你可以直接 return a + b,但是你是否可以挑战一下不这样做?
位操作 这个真不会 搜了一波
class Solution {
/*
* param a: The first integer
* param b: The second integer
* return: The sum of a and b
*/
public int aplusb(int a, int b) {
// write your code here, try to do it without arithmetic operators.
while (b!=0){
int carry = a & b;
a = a^b;
b = carry << 1;
}
return a;
}
};