// pc[i] is the populatio count of i
var pc [256]byte
//统计出o~255每个数对应二进制上1的个数
func init() {
for i := range pc {
pc[i] = pc[i/2] + byte(i&1)
// fmt.Printf("i = %d, pc[%d] = %d, pc[%d] = %d, byte(%d&1) = %d\n",
i, i, pc[i], i/2, pc[i/2], i, byte(i&1))
}
}
// PopCount return the population count (number of set bits) of x.
//将x分部分左移到底八位(0~255)
func PopCount(x uint64) int {
return int(pc[byte(x>>(0*8))] +
pc[byte(x>>(1*8))] +
pc[byte(x>>(2*8))] +
pc[byte(x>>(3*8))] +
pc[byte(x>>(4*8))] +
pc[byte(x>>(5*8))] +
pc[byte(x>>(6*8))] +
pc[byte(x>>(7*8))])
}
func init() {
for i := range pc {
pc[i] = pc[i/2] + byte(i&1)
fmt.Printf("i = %d, pc[%d] = %d, pc[%d] = %d, byte(%d&1) = %d\n",
i, i, pc[i], i/2, pc[i/2], i, byte(i&1))
}
}
// PopCount return the population count (number of set bits) of x.
func PopCount(x uint64) int {
return int(pc[byte(x>>(0*8))] +
pc[byte(x>>(1*8))] +
pc[byte(x>>(2*8))] +
pc[byte(x>>(3*8))] +
pc[byte(x>>(4*8))] +
pc[byte(x>>(5*8))] +
pc[byte(x>>(6*8))] +
pc[byte(x>>(7*8))])
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步