Skip to content

Latest commit

 

History

History
46 lines (37 loc) · 1.35 KB

README.md

File metadata and controls

46 lines (37 loc) · 1.35 KB

buffbits

License Go Reference Lint/Test

buffbits provides a buffered reader/writer that exposes bit level io access.

Install

go get github.com/larzconwell/buffbits

Byte Ordering

Data that is being written or read is done so using big-endian ordering. Below is a visual representation of the data being written and read when using the buffbits package.

var out bytes.Buffer

writer := buffbits.NewWriter(&out)
writer.Write(0b0001, 4)
writer.Write(0b00000001, 8)
writer.Flush()
err := writer.Err()
if err != nil {
    fmt.Fprintln(os.Stderr, err)
}

// Once the 2  writes and the flush occurs, the buffer `out` will contain the following bytes.
//
//  0  0  0  1  0  0  0  0    0  0  0  1  0  0  0  0
// |        byte 1        |  |        byte 2        |
// |  write 1 ||         write 2        ||  padding |

reader := buffbits.NewReader(&out)
reader.Read(6) // 0 0 0 1 0 0
reader.Read(4) // 0 0 0 0
reader.Read(2) // 0 1
reader.Read(4) // 0 0 0 0
err := reader.Err()
if err != nil {
    fmt.Fprintln(os.Stderr, err)
}