整数转换
编写一个函数,确定需要改变几个位才能将整数A转成整数B。
示例1:
输入:A = 29 (或者0b11101), B = 15(或者0b01111) 输出:2
示例2:
输入:A = 1,B = 2 输出:2
提示:
- A,B范围在[-2147483648, 2147483647]之间
示例代码:
class Solution(object):
def convertInteger(self, A, B):
"""
:type A: int
:type B: int
:rtype: int
"""
res = 0
c = A ^ B
for i in range(32):
res += c >> i & 1
return res
obj = Solution()
a = 29
b = 15
c = obj.convertInteger(a, b)
print(c)
a2 = 1
b2 = 2
c2 = obj.convertInteger(a2, b2)
print(c2)
运行结果: