Need to get sum of x and y without + operator.
I trid to sum two number using adder.If we xor x and y (x ^ y) , we will get summation without carry. From x & y we can get carry. To add this carry in summation , call add function again. but its not give answer. where is the error in my code.
def add(a,b):
if a == 0:
return b
return add(a^b, a&b)
x = 10
y = 20
print(add(10, 20))
Error:
File "main.py", line 4, in add
return add(a^b, a&b) File "main.py", line 4, in add return add(a^b, a&b) File "main.py", line 4, in add return add(a^b, a&b) File "main.py", line 4, in add return add(a^b, a&b) File "main.py", line 4, in add return add(a^b, a&b) File "main.py", line 4, in add return add(a^b, a&b) File "main.py", line 4, in add return add(a^b, a&b) File "main.py", line 4, in add return add(a^b, a&b) File "main.py", line 4, in add return add(a^b, a&b) File "main.py", line 2, in add if a == 0: RuntimeError: maximum recursion depth exceeded in comparison
add = int.__add__?