Version: v0.0.0-...-09337e1 Latest Latest

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

Go to latest
Published: Apr 17, 2020 License: MIT Imports: 3 Imported by: 0



Package bitset implements an append only bit array.

To create a Bitset and append some bits:

                                  // Bitset Contents
b := bitset.New()                 // {}
b.AppendBools(true, true, false)  // {1, 1, 0}
b.AppendBools(true)               // {1, 1, 0, 1}
b.AppendValue(0x02, 4)            // {1, 1, 0, 1, 0, 0, 1, 0}

To read values:

len := b.Len()                    // 8
v := b.At(0)                      // 1
v = b.At(1)                       // 1
v = b.At(2)                       // 0
v = b.At(8)                       // 0



This section is empty.


This section is empty.


This section is empty.


type Bitset

type Bitset struct {
	// contains filtered or unexported fields

Bitset stores an array of bits.

func Clone

func Clone(from *Bitset) *Bitset

Clone returns a copy.

func New

func New(v ...bool) *Bitset

New returns an initialised Bitset with optional initial bits v.

func NewFromBase2String

func NewFromBase2String(b2string string) *Bitset

NewFromBase2String constructs and returns a Bitset from a string. The string consists of '1', '0' or ' ' characters, e.g. "1010 0101". The '1' and '0' characters represent true/false bits respectively, and ' ' characters are ignored.

The function panics if the input string contains other characters.

func (*Bitset) Append

func (b *Bitset) Append(other *Bitset)

Append bits copied from |other|.

The new length is b.Len() + other.Len().

func (*Bitset) AppendBools

func (b *Bitset) AppendBools(bits ...bool)

AppendBools appends bits to the Bitset.

func (*Bitset) AppendByte

func (b *Bitset) AppendByte(value byte, numBits int)

AppendByte appends the numBits least significant bits from value.

func (*Bitset) AppendBytes

func (b *Bitset) AppendBytes(data []byte)

AppendBytes appends a list of whole bytes.

func (*Bitset) AppendNumBools

func (b *Bitset) AppendNumBools(num int, value bool)

AppendNumBools appends num bits of value value.

func (*Bitset) AppendUint32

func (b *Bitset) AppendUint32(value uint32, numBits int)

AppendUint32 appends the numBits least significant bits from value.

func (*Bitset) At

func (b *Bitset) At(index int) bool

At returns the value of the bit at |index|.

func (*Bitset) Bits

func (b *Bitset) Bits() []bool

Bits returns the contents of the Bitset.

func (*Bitset) ByteAt

func (b *Bitset) ByteAt(index int) byte

ByteAt returns a byte consisting of upto 8 bits starting at index.

func (*Bitset) Equals

func (b *Bitset) Equals(other *Bitset) bool

Equals returns true if the Bitset equals other.

func (*Bitset) Len

func (b *Bitset) Len() int

Len returns the length of the Bitset in bits.

func (*Bitset) String

func (b *Bitset) String() string

String returns a human readable representation of the Bitset's contents.

func (*Bitset) Substr

func (b *Bitset) Substr(start int, end int) *Bitset

Substr returns a substring, consisting of the bits from indexes start to end.

Source Files

Jump to

Keyboard shortcuts

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