bitfield

package
v1.8.10 Latest Latest
Warning

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

Go to latest
Published: May 6, 2022 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package bitfield provides support for manipulating bits in a []byte.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NumBytes

func NumBytes(length uint32) int

NumBytes calculates the number of bytes required to represent a bitfield of `length` bits.

Types

type Bitfield

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

Bitfield is described in BEP 3.

func New

func New(length uint32) *Bitfield

New creates a new Bitfield of length bits.

func NewBytes

func NewBytes(b []byte, length uint32) (*Bitfield, error)

NewBytes returns a new Bitfield from bytes. Bytes in b are not copied. Unused bits in last byte are cleared. Panics if b is not big enough to hold "length" bits.

func (*Bitfield) All

func (b *Bitfield) All() bool

All returns true if all bits are set, false otherwise.

func (*Bitfield) Bytes

func (b *Bitfield) Bytes() []byte

Bytes returns bytes in b. If you modify the returned slice the bits in b are modified too.

func (*Bitfield) Clear

func (b *Bitfield) Clear(i uint32)

Clear bit i. 0 is the most significant bit. Panics if i >= b.Len().

func (*Bitfield) Copy

func (b *Bitfield) Copy() *Bitfield

Copy returns a new copy of Bitfield.

func (*Bitfield) Count

func (b *Bitfield) Count() uint32

Count returns the count of set bits.

func (*Bitfield) Hex

func (b *Bitfield) Hex() string

Hex returns bytes as string. If not all the bits in last byte are used, they encode as not set.

func (*Bitfield) Len

func (b *Bitfield) Len() uint32

Len returns the number of bits as given to New.

func (*Bitfield) Set

func (b *Bitfield) Set(i uint32)

Set bit i. 0 is the most significant bit. Panics if i >= b.Len().

func (*Bitfield) Test

func (b *Bitfield) Test(i uint32) bool

Test bit i. 0 is the most significant bit. Panics if i >= b.Len().

Jump to

Keyboard shortcuts

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