bitstring

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2019 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package bitstring implements a fixed length bit string type and bit string manipulation functions

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrIndexOutOfRange is passed to panic if a bit index is out of range
	ErrIndexOutOfRange = errors.New("bitstring.Bitstring: index out of range")

	// ErrInvalidLength is returned when the provided bitstring length is
	// invalid.
	ErrInvalidLength = errors.New("bitstring.Bitstring: invalid length")
)

Functions

func SwapRange

func SwapRange(bs1, bs2 *Bitstring, start, length uint)

SwapRange swaps the same range of bits between bs and other.

Both Bitstring may have different length the bit with index start+lenght must be valid on both or SwapRange will panic.

Example
bs1, _ := MakeFromString("111")
bs2, _ := MakeFromString("000")
// starting from bit 2 of bs1, swap 1 bit with bs2
SwapRange(bs1, bs2, 2, 1)
fmt.Println(bs1)
Output:

011

Types

type Bitstring

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

Bitstring implements a fixed-length bit string.

Internally, bits are packed into an array of machine word integers. This implementation makes more efficient use of space than the alternative approach of using an array of booleans.

func Copy

func Copy(src *Bitstring) *Bitstring

Copy creates and returns a new Bitstring that is a copy of src.

func MakeFromString

func MakeFromString(s string) (*Bitstring, error)

MakeFromString returns the corresponding Bitstring for the given string of 1s and 0s in big endian order.

Example
// create a Bitstring from string
bitstring, _ := MakeFromString("101001")
fmt.Println(bitstring)
Output:

101001

func New

func New(length uint) *Bitstring

New creates a bit string of the specified length (in bits) with all bits initially set to zero (off).

Example
// create a 8 bits Bitstring
bitstring := New(8)
// upon creation all bits are unset
fmt.Println(bitstring)
Output:

00000000

func Random

func Random(length uint, rng *rand.Rand) *Bitstring

Random creates a Bitstring of the length l in which each bit is assigned a random value using rng.

Random randomly sets the uint32 values of the underlying slice, so it should be faster than creating a bit string and then randomly setting each individual bits.

func (*Bitstring) BigInt

func (bs *Bitstring) BigInt() *big.Int

BigInt returns the big.Int representation of bs.

Example
// create a 8 bits Bitstring
bitstring, _ := MakeFromString("100")
bi := bitstring.BigInt()
fmt.Println(bi.Int64())
Output:

4

func (*Bitstring) Bit

func (bs *Bitstring) Bit(i uint) bool

Bit returns a boolean indicating wether the bit at index i is set or not.

If i is greater than the bitstring length, Bit will panic.

Example
// create a 8 bits Bitstring
bitstring := New(8)
fmt.Println(bitstring.Bit(7))
Output:

false

func (*Bitstring) ClearBit

func (bs *Bitstring) ClearBit(i uint)

ClearBit clears the bit at index i.

If i is greater than the bitstring length, ClearBit will panic.

func (*Bitstring) Data

func (bs *Bitstring) Data() []uint

Data returns the bitstring underlying slice.

func (*Bitstring) Equals

func (bs *Bitstring) Equals(other *Bitstring) bool

Equals returns true if bs and other have the same lenght and each bit are identical, or if bs and other both point to the same Bitstring instance (i.e pointer equality).

func (*Bitstring) FlipBit

func (bs *Bitstring) FlipBit(i uint)

FlipBit flips (i.e toggles) the bit at index i.

If i is greater than the bitstring length, FlipBit will panic.

Example
// create a 8 bits Bitstring
bitstring := New(8)
bitstring.FlipBit(2)
fmt.Println(bitstring)
Output:

00000100

func (*Bitstring) Gray16

func (bs *Bitstring) Gray16(i uint) uint16

Gray16 returns the uint8 value represented by the 16 gray-coded bits starting at the given bit. It panics if there are not enough bits.

func (*Bitstring) Gray32

func (bs *Bitstring) Gray32(i uint) uint32

Gray32 returns the uint32 value represented by the 32 gray-coded bits starting at the given bit. It panics if there are not enough bits.

func (*Bitstring) Gray64

func (bs *Bitstring) Gray64(i uint) uint64

Gray64 returns the uint64 value represented by the 64 gray-coded bits starting at the given bit. It panics if there are not enough bits.

func (*Bitstring) Gray8

func (bs *Bitstring) Gray8(i uint) uint8

Gray8 returns the uint8 value represented by the 8 gray-coded bits starting at the given bit. It panics if there are not enough bits.

func (*Bitstring) Grayn

func (bs *Bitstring) Grayn(nbits, i uint) uint

Grayn returns the n-bit unsigned integer value represented by the n gray-coded bits starting at the bit index i. It panics if there are not enough bits or if n is greater than the size of a machine word.

func (*Bitstring) Int16

func (bs *Bitstring) Int16(i uint) int16

Int16 returns the int16 value represented by the 16 bits starting at the given bit. It panics if there are not enough bits.

func (*Bitstring) Int32

func (bs *Bitstring) Int32(i uint) int32

Int32 returns the int32 value represented by the 32 bits starting at the given bit. It panics if there are not enough bits.

func (*Bitstring) Int64

func (bs *Bitstring) Int64(i uint) int64

Int64 returns the int64 value represented by the 64 bits starting at the given bit. It panics if there are not enough bits.

func (*Bitstring) Int8

func (bs *Bitstring) Int8(i uint) int8

Int8 returns the int8 value represented by the 8 bits starting at the given bit. It panics if there are not enough bits.

func (*Bitstring) Intn

func (bs *Bitstring) Intn(nbits, i uint) int32

Intn returns the n-bit signed integer value represented by the n bits starting at the i. It panics if there are not enough bits or if n is greater than the size of a machine word.

func (*Bitstring) Len

func (bs *Bitstring) Len() int

Len returns the lenght if bs, that is the number of bits it contains.

Example
// create a 8 bits Bitstring
bitstring := New(8)
fmt.Println(bitstring.Len())
Output:

8

func (*Bitstring) OnesCount

func (bs *Bitstring) OnesCount() uint

OnesCount counts the number of one bits.

Example
// create a 8 bits Bitstring
bitstring := New(8)
// upon creation all bits are unset
fmt.Println(bitstring.OnesCount())
Output:

0

func (*Bitstring) SetBit

func (bs *Bitstring) SetBit(i uint)

SetBit sets the bit at index i.

If i is greater than the bitstring length, SetBit will panic.

Example
// create a 8 bits Bitstring
bitstring := New(8)
bitstring.SetBit(2)
fmt.Println(bitstring)
bitstring.ClearBit(2)
fmt.Println(bitstring)
Output:

00000100
00000000

func (*Bitstring) SetInt16

func (bs *Bitstring) SetInt16(i uint, x int16)

SetInt16 sets the 16 bits starting at i with the value of x. It panics if there are not enough bits.

func (*Bitstring) SetInt32

func (bs *Bitstring) SetInt32(i uint, x int32)

SetInt32 sets the 32 bits starting at i with the value of x. It panics if there are not enough bits.

func (*Bitstring) SetInt64

func (bs *Bitstring) SetInt64(i uint, x int64)

SetInt64 sets the 64 bits starting at i with the value of x. It panics if there are not enough bits.

func (*Bitstring) SetInt8

func (bs *Bitstring) SetInt8(i uint, x int8)

SetInt8 sets the 8 bits starting at i with the value of x. It panics if there are not enough bits.

func (*Bitstring) SetIntn

func (bs *Bitstring) SetIntn(n, i uint, x uint)

SetIntn sets the n bits starting at i with the first n bits of value x. It panics if there aren't enough bits in bs or if n is greater than the size of a machine word.

func (*Bitstring) SetUint16

func (bs *Bitstring) SetUint16(i uint, x uint16)

SetUint16 sets the 16 bits starting at i with the value of x. It panics if there are not enough bits.

func (*Bitstring) SetUint32

func (bs *Bitstring) SetUint32(i uint, x uint32)

SetUint32 sets the 32 bits starting at i with the value of x. It panics if there are not enough bits.

func (*Bitstring) SetUint64

func (bs *Bitstring) SetUint64(i uint, x uint64)

SetUint64 sets the 64 bits starting at i with the value of x. It panics if there are not enough bits.

func (*Bitstring) SetUint8

func (bs *Bitstring) SetUint8(i uint, x uint8)

SetUint8 sets the 8 bits starting at i with the value of x. It panics if there are not enough bits.

func (*Bitstring) SetUintn

func (bs *Bitstring) SetUintn(n, i uint, x uint)

SetUintn sets the n bits starting at i with the first n bits of value x. It panics if there aren't enough bits in bs or if n is greater than the size of a machine word.

func (*Bitstring) String

func (bs *Bitstring) String() string

String returns a string representation of bs in big endian order.

func (*Bitstring) Uint16

func (bs *Bitstring) Uint16(i uint) uint16

Uint16 returns the uint16 value represented by the 16 bits starting at the given bit. It panics if there are not enough bits.

func (*Bitstring) Uint32

func (bs *Bitstring) Uint32(i uint) uint32

Uint32 returns the uint32 value represented by the 32 bits starting at the given bit. It panics if there are not enough bits.

func (*Bitstring) Uint64

func (bs *Bitstring) Uint64(i uint) uint64

Uint64 returns the uint64 value represented by the 64 bits starting at the given bit. It panics if there are not enough bits.

func (*Bitstring) Uint8

func (bs *Bitstring) Uint8(i uint) uint8

Uint8 returns the uint8 value represented by the 8 bits starting at the given bit. It panics if there are not enough bits.

func (*Bitstring) Uintn

func (bs *Bitstring) Uintn(n, i uint) uint

Uintn returns the n bits unsigned integer value represented by the n bits starting at the bit index i. It panics if there aren't enough bits in bs or if n is greater than the size of a machine word. TODO: reverse order of nbits and i params

func (*Bitstring) ZeroesCount

func (bs *Bitstring) ZeroesCount() uint

ZeroesCount counts the number of zero bits.

Example
// create a 8 bits Bitstring
bitstring := New(8)
// upon creation all bits are unset
fmt.Println(bitstring.ZeroesCount())
Output:

8

Jump to

Keyboard shortcuts

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