The bitwise add 2 integers solution will hit infinite loop for negative integers. The carry will keep increasing if the bit length is not bounded.
Proposed solution:
def sum_two(self,a,b):
#max bit length, change to 32 for 32bit
max_positive = 2 ** 64
min_negative = -2 ** 64
while b != 0:
if b == max_positive:
return a ^ min_negative
a,b = a ^ b,(a&b)<<1
return a
The bitwise add 2 integers solution will hit infinite loop for negative integers. The carry will keep increasing if the bit length is not bounded. Proposed solution: