public class Solution {
/**
* @param a: An integer
* @param b: An integer
* @return: The sum of a and b
*/
public int aplusb(int a, int b) {
// write your code here
int res=0;
int xor=a^b;
int forworad=(a&b)<<1;
return forworad==0 ? xor : aplusb(xor,forworad);
}
}
1、由a^b可得按位相加後沒有進位的和及原位和;10^10=0
2、由a&b可得可以產生進位的地方;由(a&b)<<1得到進位後的值即進位和。(10&10)<<1=100
3、如何進位不爲0,則使a=原位和,b=進位和,重複上述步驟。a和b分別爲(0,100)
原位和:0^100=100 進位和: (0&100)<<1=0
如何進位爲0,則相加的結果爲原位和;如:(0&100)<<1=0 結果爲:100 4
原文:https://blog.csdn.net/qq_38002337/article/details/79510341