File tree Expand file tree Collapse file tree 1 file changed +19
-8
lines changed Expand file tree Collapse file tree 1 file changed +19
-8
lines changed Original file line number Diff line number Diff line change 66# 可能是python的的整型可以无限大的原因, 导致正数和负数的异或操作不断变成更小的负数而不会溢出
77# 使用Swift尝试了一下, 还是可以求得正数和负数的位操作相加运算的
88# -*- coding:utf-8 -*-
9+ # class Solution:
10+ # def Add(self, num1, num2):
11+ # while num2:
12+ # sum = num1 ^ num2
13+ # carry = (num1 & num2) << 1
14+ # num1 = sum
15+ # num2 = carry
16+ # return num1
17+ # s = Solution()
18+ # print(s.Add(4, 2))
19+ # -*- coding:utf-8 -*-
20+ # 通过每次对num1进行与操作保证是一个32位的整形
21+ # 因此最后我们可以判断符号位是否为1做处理
922class Solution :
1023 def Add (self , num1 , num2 ):
11- while num2 :
12- sum = num1 ^ num2
13- carry = (num1 & num2 ) << 1
14- num1 = sum
15- num2 = carry
16- return num1
17- s = Solution ()
18- print (s .Add (4 , 2 ))
24+ # write code here
25+ while num2 != 0 :
26+ temp = num1 ^ num2
27+ num2 = (num1 & num2 ) << 1
28+ num1 = temp & 0xFFFFFFFF
29+ return num1 if num1 >> 31 == 0 else num1 - 4294967296
You can’t perform that action at this time.
0 commit comments