chinademon 2020-05-06
# -*- coding:utf-8 -*- class Solution: def Add(self, num1, num2): # write code here pos_sum = num1 while num2: pos_sum = (num1^num2)& 0xffffffff num2 = ((num1&num2)<<1)& 0xffffffff num1 = pos_sum if pos_sum <= 0x7fffffff: return pos_sum else: return ~(pos_sum ^ 0xffffffff) note: 0xffffffff表示-1 1111 1111 1111 1111 1111 1111 1111 1111(第一个1是符号位) 0x7fffffff表示最大正数,0111 1111 1111 1111 1111 1111 1111 1111(0是符号位)对0取反为-1,对1取反为-2