I am learning python by trying to solve question as follows.
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.
The following solution that I have came up works with positive integers but it does not work if a= -1 and b =1.
I wonder how do you handle negative values.
class Solution(object):
def getSum(self, a, b):
"""
:type a: int
:type b: int
:rtype: int
"""
while b != 0:
carry = a&b
a= a^b
b= carry<<1
return a