Adding two numbers without + operator (Clarification)
问题 I know that we can use the logic of binary adder where Sum = a XOR b and Carry = a AND b I have also got a solution: int add(int a, int b) { if(b == 0) return sum; sum = a ^ b; carry = (a & b) << 1; return add(sum,carry); } What I don't understand here is why is the carry bit shifted, or multiplied by 2 during each recursion? 回答1: I find this a bit tricky to explain, but here's an attempt; think bit by bit addition, there are only 4 cases; 0+0=0 0+1=1 1+0=1 1+1=0 (and generates carry) The two