-
Notifications
You must be signed in to change notification settings - Fork 0
/
bits.go
62 lines (55 loc) · 1.23 KB
/
bits.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package codec
import "math/bits"
// minByteSize returns the minimum number of bytes required to represent v
func minByteSize(v uint64) int {
if v == 0 {
return 1
}
size := bits.Len64(v)
if size&7 == 0 {
return size >> 3
}
return size>>3 + 1
}
func minByteSizeWithMeta(v uint64, metaSize int) int {
size := bits.Len64(v)
if size == 0 {
size++
}
size += metaSize
if size&7 == 0 {
return size >> 3
}
return size>>3 + 1
}
// toMinBytes returns the minimum number of bytes required to represent v
// and its byte representation in big endian
func toMinBytes(v uint64) (int, []byte) {
size := minByteSize(v)
bytes := make([]byte, size, size)
for i, j := 0, size; j > 0; i++ {
j--
bytes[i] = byte(v >> (j << 3))
}
return size, bytes
}
// toMinBytes returns the minimum number of bytes required to represent v
func asMinBytes(v uint64) []byte {
_, b := toMinBytes(v)
return b
}
// toBytes returns the v's byte representation of the given size in big endian
func toBytes(v uint64, size int) []byte {
bytes := make([]byte, size, size)
for i := 0; size > 0; i++ {
size--
bytes[i] = byte(v >> (size << 3))
}
return bytes
}
func toUint(i int64) uint64 {
if i >= 0 {
return uint64(i) << 1
}
return uint64(^i<<1) | 1
}