规律:二进制中1的个数,比如100(对应十进制的4)求它中1的个数就 100 & (100-001)就行
package main
import (
"fmt"
)
func main() {
num := 11
fmt.Printf("bit num %v", count_bit(num))
}
//检测二进制中1的个数
func count_bit(num int) int {
count := 0
for num > 0 {
num = num & (num - 1)
count++
}
return count
}