]> Sergey Matveev's repositories - btrtrc.git/blob - dht/bitcount.go
a21c8c0bceb53ffffeec8b5e16808279eee5ef09
[btrtrc.git] / dht / bitcount.go
1 package dht
2
3 import (
4         "math/big"
5 )
6
7 // TODO: The bitcounting is a relic of the old and incorrect distance
8 // calculation. It is still useful in some tests but should eventually be
9 // replaced with actual distances.
10
11 // How many bits?
12 func bitCount(n big.Int) int {
13         var count int = 0
14         for _, b := range n.Bytes() {
15                 count += int(bitCounts[b])
16         }
17         return count
18 }
19
20 // The bit counts for each byte value (0 - 255).
21 var bitCounts = []int8{
22         // Generated by Java BitCount of all values from 0 to 255
23         0, 1, 1, 2, 1, 2, 2, 3,
24         1, 2, 2, 3, 2, 3, 3, 4,
25         1, 2, 2, 3, 2, 3, 3, 4,
26         2, 3, 3, 4, 3, 4, 4, 5,
27         1, 2, 2, 3, 2, 3, 3, 4,
28         2, 3, 3, 4, 3, 4, 4, 5,
29         2, 3, 3, 4, 3, 4, 4, 5,
30         3, 4, 4, 5, 4, 5, 5, 6,
31         1, 2, 2, 3, 2, 3, 3, 4,
32         2, 3, 3, 4, 3, 4, 4, 5,
33         2, 3, 3, 4, 3, 4, 4, 5,
34         3, 4, 4, 5, 4, 5, 5, 6,
35         2, 3, 3, 4, 3, 4, 4, 5,
36         3, 4, 4, 5, 4, 5, 5, 6,
37         3, 4, 4, 5, 4, 5, 5, 6,
38         4, 5, 5, 6, 5, 6, 6, 7,
39         1, 2, 2, 3, 2, 3, 3, 4,
40         2, 3, 3, 4, 3, 4, 4, 5,
41         2, 3, 3, 4, 3, 4, 4, 5,
42         3, 4, 4, 5, 4, 5, 5, 6,
43         2, 3, 3, 4, 3, 4, 4, 5,
44         3, 4, 4, 5, 4, 5, 5, 6,
45         3, 4, 4, 5, 4, 5, 5, 6,
46         4, 5, 5, 6, 5, 6, 6, 7,
47         2, 3, 3, 4, 3, 4, 4, 5,
48         3, 4, 4, 5, 4, 5, 5, 6,
49         3, 4, 4, 5, 4, 5, 5, 6,
50         4, 5, 5, 6, 5, 6, 6, 7,
51         3, 4, 4, 5, 4, 5, 5, 6,
52         4, 5, 5, 6, 5, 6, 6, 7,
53         4, 5, 5, 6, 5, 6, 6, 7,
54         5, 6, 6, 7, 6, 7, 7, 8,
55 }