Here is the reference implementation I got, the confusion is, I think there is no need for recursion. I post both code and my thought below here, any insights are appreciated. Please feel free to correct me if I am wrong.
BTW, for the two implementation differences, refer to line 5th. Thanks.
The question is, whether recursive is needed? Thanks.
Reference implementation,
1 int add_no_arithm(int a, int b) {
2 if (b == 0) return a;
3 int sum = a ^ b; // add without carrying
4 int carry = (a & b) << 1; // carry, but don’t add
5 return add_no_arithm(sum, carry); // recurse
6 }
Another implementation in my thought,
1 int add_no_arithm(int a, int b) {
2 if (b == 0) return a;
3 int sum = a ^ b; // add without carrying
4 int carry = (a & b) << 1; // carry, but don’t add
5 return sum ^ carry;
6 }
BTW, tried 8+8 works for me in Python,
a = 8
b = 8
sum = a ^ b
carry = (a & b) << 1
print sum^carry #16
thanks in advance, Lin