Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.

Example:
Given a = 1 and b = 2, return 3.


Credits:Special thanks to @fujiaozhu for adding this problem and creating all test cases.

Difficulty: introductory

Starter code:

class Solution:
    def getSum(self, a: int, b: int) -> int:
        

Solutions:
class Solution:
     def getSum(self, a, b):
         max = 0x7FFFFFFF
         mask = 0xFFFFFFFF
         while b != 0:
             a, b = (a ^ b) & mask, ((a & b) << 1) & mask
         return a if a <= max else ~(a ^ mask)

class Solution:
     def getSum(self, a, b):
         """
         :type a: int
         :type b: int
         :rtype: int
         """
         if a == 0:
             return b
         if b == 0:
             return a
         
         MAX = 0x7FFFFFFF
         MIN = 0x80000000
         mask = 0xFFFFFFFF
         while b:
             carry = a & b
             a = (a ^ b) & mask
             b = carry << 1 & mask
         
         return a if a <= MAX else ~(a ^ mask)

class Solution:
     def getSum(self, a, b):
         """
         :type a: int
         :type b: int
         :rtype: int
         """
         return sum([a,b])

class Solution:
     def getSum(self, a, b):
         """
         :type a: int
         :type b: int
         :rtype: int
         """
         if a == 0:
             return b
         if b == 0:
             return a
         
         MAX = 0x7FFFFFFF
         MIN = 0x80000000
         mask = 0xFFFFFFFF
         while b:
             a, b = (a ^ b) & mask, ((a & b) << 1) & mask
         
         return a if a <= MAX else ~(a ^ mask)

class Solution:
     def getSum(self, a, b):
         """
         :type a: int
         :type b: int
         :rtype: int
         """
         MAX = 0x7FFFFFFF
         mask = 0xFFFFFFFF
         while b != 0:
             a, b = (a ^ b) & mask, ((a & b) << 1) & mask
         return a if a <= MAX else ~(a ^ mask)

class Solution:
     def getSum(self, a, b):
         """
         :type a: int
         :type b: int
         :rtype: int
         """
         MAX_INT = 0x7FFFFFFF # from zero to python 32 bits maximum positive integer
         MASK = 0x100000000
         
         #(a ^ b) is "summing" the bits of a and b
         #For example (where D is decimal and B is binary), 20D == 10100B, and 9D = 1001B
         # Enter the (a & b) << 1 term. This term represents the "carry" for each position. On the next iteration of the while loop, we add in the carry from the previous loop.       
         
         #while b is not 0
         while b: 
             a,b= (a^b) % MASK, ((a&b) <<1) % MASK
         return a if a <= MAX_INT else ~((a&MAX_INT)^ MAX_INT)
 
     
     
     # All the masks are doing is ensuring that the value is an integer, because your code even has comments stating that a, b, and the return type are of type int. Thus, since the maximum possible int (32 bits) is 2147483647. So, if you add 2 to this value, like you did in your example, the int overflows and you get a negative value. You have to force this in Python, because it doesn't respect this int boundary that other strongly typed languages like Java and C++ have defined.


class Solution(object):
     
     def ca(self, a, b):
         s = [[] for i in range(a)]
 
         for i in range(b):
             s.append(0)
         return len(s)
 
     def cal(self, big, small):
         aa = [[] for i in range(big)]
         for i in range(small):
             aa.pop()
         return len(aa)
     
     def getSum(self, a, b):
         if a==2147483647:
             return -1
         if a>0 and b <0:
             a, b = b, a
 
         if a==0:
             return b
         if b==0:
             return a
 
         if a<0 and b<0:
             return -self.ca(abs(a), abs(b))
         elif a>0 and b>0:
             return self.ca(a,b)
 
 
         elif a<0 and b>0:
             if abs(a) > b:
                 return -self.cal(abs(a), b)
             elif abs(a)==b:
                 return 0
             else:
                 return self.cal(b, abs(a))
 
 


class Solution:
     def getSum(self, a, b):
         """
         :type a: int
         :type b: int
         :rtype: int
         """
         max_value = 0x7FFFFFFF
         masking = 0xFFFFFFFF
         while b != 0:
 
             carry = a & b
             a = (a ^ b) & masking
             b = (carry << 1) & masking
 
         return a if a <= max_value else ~(a ^ masking)

class Solution:
     def getSum(self, a, b):
         """
         :type a: int
         :type b: int
         :rtype: int
         """
         return a+b