bits

package
v1.0.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2023 License: Unlicense Imports: 3 Imported by: 0

Documentation

Overview

Package bits provides bit access operations and binary decoding algorithms.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeZigZag added in v1.0.10

func DecodeZigZag(x uint32) int32

DecodeZigZag decodes a ZigZag encoded integer and returns it.

Examples of ZigZag encoded values on the left and decoded values on the right:

0 =>  0
1 => -1
2 =>  1
3 => -2
4 =>  2
5 => -3
6 =>  3

ref: https://developers.google.com/protocol-buffers/docs/encoding

func EncodeZigZag added in v1.0.10

func EncodeZigZag(x int32) uint32

EncodeZigZag encodes a given integer to ZigZag-encoding.

Examples of integer input on the left and corresponding ZigZag encoded values on the right:

 0 => 0
-1 => 1
 1 => 2
-2 => 3
 2 => 4
-3 => 5
 3 => 6

ref: https://developers.google.com/protocol-buffers/docs/encoding

func IntN added in v1.0.9

func IntN(x uint64, n uint) int64

IntN returns the signed two's complement of x with the specified integer bit width.

Examples of unsigned (n-bit width) x values on the left and decoded values on the right:

0b011 -> 3
0b010 -> 2
0b001 -> 1
0b000 -> 0
0b111 -> -1
0b110 -> -2
0b101 -> -3
0b100 -> -4

func WriteUnary

func WriteUnary(bw *bitio.Writer, x uint64) error

WriteUnary encodes x as an unary coded integer, whose value is represented by the number of leading zeros before a one.

Examples of unary coded binary on the left and decoded decimal on the right:

0 => 1
1 => 01
2 => 001
3 => 0001
4 => 00001
5 => 000001
6 => 0000001

Types

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

A Reader handles bit reading operations. It buffers bits up to the next byte boundary.

func NewReader

func NewReader(r io.Reader) *Reader

NewReader returns a new Reader that reads bits from r.

func (*Reader) Read

func (br *Reader) Read(n uint) (x uint64, err error)

Read reads and returns the next n bits, at most 64. It buffers bits up to the next byte boundary.

func (*Reader) ReadUnary

func (br *Reader) ReadUnary() (x uint64, err error)

ReadUnary decodes and returns an unary coded integer, whose value is represented by the number of leading zeros before a one.

Examples of unary coded binary on the left and decoded decimal on the right:

1       => 0
01      => 1
001     => 2
0001    => 3
00001   => 4
000001  => 5
0000001 => 6

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL