https://github.com/32bitkid/bitreader
Simple Bitreader for Golang.
https://github.com/32bitkid/bitreader
Last synced: 5 months ago
JSON representation
Simple Bitreader for Golang.
- Host: GitHub
- URL: https://github.com/32bitkid/bitreader
- Owner: 32bitkid
- License: mit
- Created: 2014-06-08T22:05:17.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2019-12-27T23:45:32.000Z (over 6 years ago)
- Last Synced: 2025-08-13T17:45:15.304Z (10 months ago)
- Language: Go
- Size: 20.5 KB
- Stars: 21
- Watchers: 3
- Forks: 11
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bitreader
Provides basic interfaces to read and traverse an `io.Reader` as a stream of bits, rather than a stream of bytes.
[](https://godoc.org/github.com/32bitkid/bitreader)
## Installation
```bash
$ go get github.com/32bitkid/bitreader
```
## Examples
Ever wanted to count the number of 0 bits from the start of a file?
```go
package main
import (
"os"
"fmt"
"github.com/32bitkid/bitreader"
)
func main() {
file, _ := os.Open("file")
r := bitreader.NewReader(file)
n := 0
for {
val, err := r.Read1()
if err != nil || val == true {
break
}
n += 1
}
fmt.Printf("The file starts with %d off bits", n)
}
```
But seriously, this is used for parsing densely packed binary formats where data may not be byte aligned. For example, decoding values packed with [Huffman Coding](https://en.wikipedia.org/wiki/Huffman_coding).