371 - Sum Of Two Integers
Written on November 7, 2017
Tweet
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
class Solution(object):
def getSum(self, a, b):
"""
:type a: int
:type b: int
:rtype: int
"""
MAX = 2 ** 31 - 1
MIN = - 2 * 31
# mask to get last 32 bits
mask = 0xFFFFFFFF
while b != 0:
a, b = (a ^ b) & mask, ((a & b) << 1) & mask
# ^ get different bits and & gets double 1s, << moves carry
# if a is negative, get a's 32 bits complement positive first
# then get 32-bit positive's Python complement negative
return a if a <= MAX else ~ (a ^ mask)