-
Notifications
You must be signed in to change notification settings - Fork 0
/
371.py
55 lines (44 loc) · 1.11 KB
/
371.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = ['"wuyadong" <[email protected]>']
class Solution(object):
def getSum(self, a, b):
"""
:type a: int
:type b: int
:rtype: int
"""
x = 1
p = 0
result = 0
while x <= 0x80000000:
a1 = x & a
b1 = x & b
# plus with p
if p == 0:
if a1 | b1 == 0:
r1 = 0
elif a1 & b1 == 0:
r1 = 1
else:
r1 = 0
p = 1
else:
if a1 | b1 == 0:
p = 0
r1 = 1
elif a1 & b1 == 0:
r1 = 0
p = 1
else:
r1 = 1
p = 1
if r1 == 1:
result |= x
x <<= 1
if result & 0x8000000 == 0:
return result
else:
return ~(result ^ 0xFFFFFFFF)
if __name__ == "__main__":
print Solution().getSum(-2, -3)