标签:www. bit code problem while ati 注意 int rip
Topic: Math&Bit Manipulation
Determine the number of bits required to flip if you want to convert integer n to integer m.
Example
Given n = 31 (11111), m = 14 (01110), return 2.
Notice
Both n and m are 32-bit integers.
class Solution:
"""
@param a: An integer
@param b: An integer
@return: An integer
"""
def bitSwapRequired(self, a, b):
# write your code here
ff=pow(2,32)#注意负数
a_r = (a + ff)%ff
b_r = (b + ff)%ff
count = 0
while ((a_r or b_r) != 0):
a_b = a_r % 2
b_b = b_r % 2
count += (a_b != b_b)
a_r = a_r // 2
b_r = b_r // 2
return count
标签:www. bit code problem while ati 注意 int rip
原文地址:https://www.cnblogs.com/siriusli/p/10358604.html