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
The bitwise XOR operator
^
is only equivalent to the addition operator+
if there is no binary carrying in the sums. If there is binary carrying, then they are not equivalent. For example,8 + 7
equals8 ^ 7
equals15
, but8 + 8
is16
while8 ^ 8
is0
.Even if you have calculated the sum-no-carry and the carry-no-sum correctly, what if those two numbers, when added, would produce a binary carry? Then your
^
operator at the end would be incorrect. Without the+
operator, the only option I see is to recurse, to add those numbers. This will recur until one of the numbers is 0.Example:
sum = a^b
18 ^ 6 is20
.carry = (a & b) << 1
18 & 6 is 2, bit shift left 1 is4
.return sum ^ carry
20 ^ 4 is16
, incorrect (18 + 6 = 24).The second approach doesn't work with
1 + 3
.Here are the steps
Just doing
^
at the last step is not enough, as there may be a carry in that sum.Yes, it is. You can see this for yourself by experimenting with other numbers, instead of just 8 + 8. For example, try 21 and 15, without recursion this gives output of 26.