(a+b问题)两数相加不使用算数运算符
class Solution(object):
def x_plus_y(self, x, y):
"""a+b问题"""
self.x_AND_y = (x & y) << 1
self.x_XOR_y = x ^ y
while True:
self.new_XOR = self.x_AND_y ^ self.x_XOR_y
self.new_AND = (self.x_AND_y & self.x_XOR_y) << 1
self.x_XOR_y = self.new_XOR ^ self.new_AND
self.x_AND_y = (self.new_XOR & self.new_AND) << 1
if self.new_AND == 0 or self.x_AND_y == 0:
if self.new_XOR > self.x_XOR_y:
print(self.new_XOR)
else:
print(self.x_XOR_y)
break
a_add_b = Solution()
a_add_b.x_plus_y(112, 111)
"""
输出结果:
223
"""