python中没有+运算符的求和

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))

错误:

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
评论
xsint
xsint

您错过了一个条件。如果b == 0,则返回a。

def add(a,b):
    if a == 0:
        return b
    if b == 0:
        return a
    return add(a^b, a&b)

x = 10
y = 20
print(add(10, 20))
点赞
评论
萌了个兔
萌了个兔

您忘记了基本情况的一半:

def add(a,b):
    if a == 0 or b==0:   # if either one is zero
        return a or b         # return the non-zero one (or 0 if both are 0)
    return add(a^b, a&b)

x = 10
y = 20
print(add(10, 20))

版画

30
点赞
评论