Golang程序计算将给定整数转换为另一个整数的翻转次数。
例子
考虑两个数字m=65=>01000001和n=80=>01010000
翻转的位数是2。
解决这个问题的方法
步骤1-将两个数字都转换为比特。
步骤2-计数位数被翻转。
示例
package main
import (
"fmt"
"strconv"
)
func FindBits(x, y int) int{
n := x ^ y
count := 0
for ;n!=0; count++{
n = n & (n-1)
}
return count
}
func main(){
x := 65
y := 80
fmt.Printf("Binary of %d is: %s.\n", x, strconv.FormatInt(int64(x), 2))
fmt.Printf("Binary of %d is: %s.\n", y, strconv.FormatInt(int64(y), 2))
fmt.Printf("The number of bits flipped is %d\n", FindBits(x, y))
}输出结果Binary of 65 is: 1000001. Binary of 80 is: 1010000. The number of bits flipped is 2
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下