https://github.com/felix/bitarray
[mirror] BitArray, a bit array with packing.
https://github.com/felix/bitarray
array array-manipulations bits go
Last synced: about 1 month ago
JSON representation
[mirror] BitArray, a bit array with packing.
- Host: GitHub
- URL: https://github.com/felix/bitarray
- Owner: felix
- License: mit
- Created: 2019-07-16T22:55:46.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-07-22T07:23:48.000Z (over 4 years ago)
- Last Synced: 2024-06-20T16:39:29.236Z (over 1 year ago)
- Topics: array, array-manipulations, bits, go
- Language: Go
- Homepage: https://src.userspace.com.au/bitarray/about
- Size: 46.9 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# BitArray
This package provides a bit array structure with sub-byte methods like packing
and shifting for arrays of bits of arbitrary length.
This is not useful for set operations. It facilitates encoding data that is not
necessarily aligned to 8 bits. It was originally designed to cater to the
packed encoding required for the ISO/IEC 20248 digital signature format.
## Usage
```go
import (
"fmt"
"src.userspace.com.au/bitarray"
)
func main() {
ba := &bitarray.BitArray{}
// Add single bits, never truncated
ba.AddBit(1)
ba.AddBit(0)
ba.AddBit(1)
fmt.Println(ba.Len()) // => 3
fmt.Printf("%08b\n", ba.Bytes()) // => [10100000]
// Add with truncated leading padding
ba.Add(5)
fmt.Printf("%08b\n", ba.Bytes()) // => [10110100]
// Add with fixed length
ba.AddN(5, 10)
fmt.Printf("%08b\n", ba.Bytes()) // => [10110100 00000101]
r := bitarray.NewReader(ba)
var out uint
r.ReadBits(&out, 16)
fmt.Printf("%08b\n", out) // => 1011010000000101
ba.ShiftL(2)
fmt.Println(ba.String()) // => [11010000 000101--]
ba2, _ := ba.Slice(9, 6)
fmt.Println(ba2.String()) // => [000101--]
}
```