Write a function that add two numbers A and B. You should not use + or any arithmetic operators.
Have you met this question in a real interview? Yes
Example
Given a=1 and b=2 return 3
Note
There is no need to read data from standard input stream. Both parameters are given in function aplusb, you job is to calculate the sum and return it.
Challenge
Of course you can just return a + b to get accepted. But Can you challenge not do it like that?
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) {
int sum = a^b; //求和
int carry = (a&b)<<1;//进位
if(carry!=0)
return aplusb(sum, carry);
return sum;
}
}