mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-09-13 07:02:16 +00:00
good good study, day day up!
This commit is contained in:
31
vendor/gopl.io/ch2/popcount/main.go
generated
vendored
Normal file
31
vendor/gopl.io/ch2/popcount/main.go
generated
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
// See page 45.
|
||||
|
||||
// (Package doc comment intentionally malformed to demonstrate golint.)
|
||||
//!+
|
||||
package popcount
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
// PopCount returns 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))])
|
||||
}
|
||||
|
||||
//!-
|
Reference in New Issue
Block a user