371. Sum of Two Integers - LeetCode
class Solution1:
def getSum(self, a: int, b: int) -> int:
MASK, xor, carry = 1023, a ^ b, (a & b) << 1
if carry & MASK:
return self.getSum(xor, carry)
return xor & MASK if carry else xor
class Solution:
def getSum(self, a: int, b: int) -> int:
MASK = 1023
while b & MASK:
a, b = a ^ b, (a & b) << 1
return a & MASK if b else a