Package Main
Import (
"FMT"
)
Pc[i] is the population count of I.
var pc [256]byte
Func 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%v\n%x\n%b\n\n", Tmpnum, Tmpnum, Tmpnum)
Bytenum: = Byte (Tmpnum)
Fmt. Printf ("bytenum:\n%v\n%x\n%b\ncount:%d\n\n", Bytenum, Bytenum, 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 ("\n\n\n\n\n")
Fmt. Println (Popcount (23495631783903473))
Fmt. Println (Analyzepopcount (23495631783903473))
}