您的位置:首页 > 其它

A + B Problem

2016-07-06 02:21 344 查看
Write a function that add two numbers A and B. You should not use
+
or any arithmetic operators.

分析:

典型的Bit Operation.

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) {
if (a == 0) return b;
int sum = a ^ b;
int carry = (a & b) << 1;
return aplusb(carry, sum);
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: