1 parent 98c144f commit ddc8958Copy full SHA for ddc8958
1 file changed
flip_bits.py
@@ -0,0 +1,23 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+class Solution:
4
+ """
5
+ @param a, b: Two integer
6
+ return: An integer
7
8
+ def bitSwapRequired(self, a, b):
9
+ # write your code here
10
+ a ^= b
11
+ if a > 0:
12
+ return self.ones(a)
13
+ else:
14
+ return 32 - self.ones(abs(a) - 1)
15
16
+ def ones(self, n):
17
+ ret = 0
18
+ n &= 0xffffffff
19
+ while n != 0:
20
+ if n & 1:
21
+ ret += 1
22
+ n >>= 1
23
+ return ret
0 commit comments