-
Notifications
You must be signed in to change notification settings - Fork 177
/
utils.go
50 lines (44 loc) · 1.54 KB
/
utils.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
package bitutils
// ReadBit returns the bit at index `idx` in the byte array `b` (big endian)
// The function panics, if the byte slice is too short.
func ReadBit(b []byte, idx int) int {
byteValue := int(b[idx>>3])
idx &= 7
return (byteValue >> (7 - idx)) & 1
}
// WriteBit assigns value `v` to the bit at index `i` in the byte array `b`.
// The function panics, if the byte slice is too short. We follow the common
// convention of converting between integer and boolean/bit values:
// - int value == 0 <=> false <=> bit 0
// - int value != 0 <=> true <=> bit 1
func WriteBit(b []byte, i int, value int) {
if value == 0 {
ClearBit(b, i)
} else {
SetBit(b, i)
}
}
// SetBit sets the bit at index `i` in the byte array `b`, i.e. it assigns
// value 1 to the bit. The function panics, if the byte slice is too short.
func SetBit(b []byte, i int) {
byteIndex := i >> 3
i &= 7
mask := byte(1 << (7 - i))
b[byteIndex] |= mask
}
// ClearBit clears the bit at index `i` in the byte slice `b`, i.e. it assigns
// value 0 to the bit. The function panics, if the byte slice is too short.
func ClearBit(b []byte, i int) {
byteIndex := i >> 3
i &= 7
mask := byte(1 << (7 - i))
b[byteIndex] &= ^mask
}
// MakeBitVector allocates a byte slice of minimal size that can hold numberBits.
func MakeBitVector(numberBits int) []byte {
return make([]byte, MinimalByteSliceLength(numberBits))
}
// MinimalByteSliceLength returns the minimal length of a byte slice that can store n bits.
func MinimalByteSliceLength(n int) int {
return (n + 7) >> 3
}