加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

golang 统计uint64 数字二进制存储中1的数量

发布时间:2020-12-16 09:34:54 所属栏目:大数据 来源:网络整理
导读:package mainimport (?? ?"fmt")// pc[i] is the population count of i.var pc [256]bytefunc init() {?? ?for i := range pc {?? ??? ?pc[i] = pc[i/2] + byte(i1)?? ?}}func PopCount(x uint64) int {?? ?return int(pc[byte(x(0*8))] +?? ??? ?pc[byte(x

package mainimport (?? ?"fmt")// pc[i] is the population count of i.var pc [256]bytefunc init() {?? ?for i := range pc {?? ??? ?pc[i] = pc[i/2] + byte(i&1)?? ?}}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 AnalyzePopCount(x uint64) int {?? ?count := 0?? ?var i uint?? ?for i = 0; i < 8; i++ {?? ??? ?tmpNum := x >> (i * 8)?? ??? ?fmt.Printf("tmpNum:n%vn%xn%bnn",tmpNum,tmpNum)?? ??? ?byteNum := byte(tmpNum)?? ??? ?fmt.Printf("byteNum:n%vn%xn%bncount:%dnn",byteNum,pc[byteNum])?? ??? ?count += int(pc[byteNum])?? ?}?? ?return count}func main() {?? ?/*?? ??? ?fmt.Println(2 << 1)?? ??? ?fmt.Println(2<<1 + 1)?? ??? ?fmt.Println(4 >> 1)?? ?*/?? ?fmt.Println(pc[255])?? ?for i := range pc {?? ??? ?fmt.Printf("%v,",pc[i])?? ??? ?if (i % 16) == 15 {?? ??? ??? ?fmt.Println()?? ??? ?}?? ?}?? ?pc[0] = 255?? ?fmt.Println(pc[0])?? ?pc[0] = 254?? ?fmt.Println(pc[0])?? ?//fmt.Printf("nnnnn")?? ?//fmt.Println(PopCount(23495631783903473))?? ?//fmt.Println(AnalyzePopCount(23495631783903473))}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读