Flip Bits
Question
Determine the number of bits required to flip if you want to convert integer n to integer m.
Thoughts
很简单,但是要注意在有负数的情况下不能用while x
, 可以假设为32位整数
Solution
class Solution:
"""
@param a, b: Two integer
return: An integer
"""
def bitSwapRequired(self, a, b):
# write your code here
x = a ^ b
num = 0
number = 32
while number > 0:
if x & 1 > 0:
num += 1
x = x >> 1
number -= 1
return num