Browse Source

popcount

branchMac
dewjjj 10 months ago
parent
commit
3356c67efe
  1. 1
      README.md
  2. 19
      popcount/popcount.go
  3. 11
      test/test.go

1
README.md

@ -16,3 +16,4 @@
|12|gcd|最大公约数|
|13|type|类型别名|
|14|tempconv&kelvins|开尔文温度转换|
|15|popcount&test|置位数量|

19
popcount/popcount.go

@ -0,0 +1,19 @@
package popcount
var pc [256]byte
func init() {
for i := range pc {
pc[i] = pc[i/2] + byte(1&i)
}
}
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))])
}

11
test/test.go

@ -0,0 +1,11 @@
package main
import (
"fmt"
"popcount"
)
func main() {
res := popcount.PopCount(7976543532654546)
fmt.Println(res)
}
Loading…
Cancel
Save