-
Notifications
You must be signed in to change notification settings - Fork 52
/
bitarray.go
45 lines (39 loc) · 1.03 KB
/
bitarray.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
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
// Package util implements utilities to better support Fec decoding / encoding.
package util
// BitArray provides support for bitmask manipulations.
type BitArray struct {
Lo uint64 // leftmost 64 bits
Hi uint64 // rightmost 64 bits
}
// SetBit sets a bit to the specified bit value on the bitmask.
func (b *BitArray) SetBit(bitIndex uint32) {
if bitIndex < 64 {
b.Lo |= uint64(0b1) << (63 - bitIndex)
} else {
hiBitIndex := bitIndex - 64
b.Hi |= uint64(0b1) << (63 - hiBitIndex)
}
}
// Reset clears the bitmask.
func (b *BitArray) Reset() {
b.Lo = 0
b.Hi = 0
}
// GetBit returns the bit value at a specified index of the bitmask.
func (b *BitArray) GetBit(bitIndex uint32) uint8 {
if bitIndex < 64 {
result := (b.Lo & (uint64(0b1) << (63 - bitIndex)))
if result > 0 {
return 1
}
return 0
}
hiBitIndex := bitIndex - 64
result := (b.Hi & (uint64(0b1) << (63 - hiBitIndex)))
if result > 0 {
return 1
}
return 0
}