题目:输入两个整数m和n,计算m需要改变多少位才能得到n?计算需要改变m的二进制表示中的多少位才能得到n。5的二进制1111,1的二进制1 ,需要改变3位。
func exchangeToSame(firstNum:Int,secondNum:Int) -> Int {
var result:Int = firstNum ^ secondNum
var count:Int = 0
while result != 0 {
count += 1
result = result & (result-1)
}
return count
}
let first:Int = 15
let next:Int = 1
let changeNum:Int = self.exchangeToSame(firstNum: first, secondNum: next)
print("FlyElephant-改变\(changeNum)位")