help pls - doesn't work for neg num


  • 0
    S
    class Solution(object):
        def getSum(self, a, b):
            """
            :type a: int
            :type b: int
            :rtype: int
            """
            carry = 0
            ans = 0
            for i in range(32):
                a1 = (a>>i) & 0x1
                b1 = (b>>i) & 0x1
                s = a1 ^ b1 ^ carry
                ans |= (s << i)
                carry = a1 & b1 | (a1 ^ b1) & carry
            
            return ans
    

    Try -1 and -2. Ans shd be -3 but i get 4294967276


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.