1
2
3
4
5
6
7
8
9
10 package main
11
12 const b26 uint64 = 0x022fdd63cc95386d
13
14 var bitPos [64]int
15
16 func init() {
17 for p := uint(0); p < 64; p++ {
18 bitPos[b26<<p>>58] = int(p)
19 }
20 }
21
22 func MinPos(w uint64) int {
23 if w == 0 {
24 panic("bit: MinPos(0) undefined")
25 }
26 return bitPos[((w&-w)*b26)>>58]
27 }
28
29 func main() {
30 const one = uint64(1)
31 for i := 0; i < 64; i++ {
32 if MinPos(1<<uint(i)) != i {
33 println("i =", i)
34 panic("MinPos(1<<uint(i)) != i")
35 }
36 }
37 }
38
View as plain text