zoukankan      html  css  js  c++  java
  • 统计uint64的数对应二进制数的1的个数

    // 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 ",
                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 ",
                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))])
    }

  • 相关阅读:
    mysql进阶
    浅谈数据库查询操作时的顺序
    Problem C Emergency Evacuation 一道思维题
    c++随机生成树
    洛谷 P4408 [NOI2003]逃学的小孩
    UVA11300 Spreading the Wealth
    洛谷 P3574 [POI2014]FAR-FarmCraft
    洛谷 P2882 [USACO07MAR]Face The Right Way G
    JSOI BZOJ4472 salesman
    CF 1912 A NEKO's Maze Game
  • 原文地址:https://www.cnblogs.com/barfoo/p/6666576.html
Copyright © 2011-2022 走看看