Documentation ¶
Overview ¶
Package bitstring implements a fixed length bit string type and bit string manipulation functions
Index ¶
- Variables
- func SwapRange(bs1, bs2 *Bitstring, start, length uint)
- type Bitstring
- func (bs *Bitstring) BigInt() *big.Int
- func (bs *Bitstring) Bit(i uint) bool
- func (bs *Bitstring) ClearBit(i uint)
- func (bs *Bitstring) Data() []uint
- func (bs *Bitstring) Equals(other *Bitstring) bool
- func (bs *Bitstring) FlipBit(i uint)
- func (bs *Bitstring) Gray16(i uint) uint16
- func (bs *Bitstring) Gray32(i uint) uint32
- func (bs *Bitstring) Gray64(i uint) uint64
- func (bs *Bitstring) Gray8(i uint) uint8
- func (bs *Bitstring) Grayn(nbits, i uint) uint
- func (bs *Bitstring) Int16(i uint) int16
- func (bs *Bitstring) Int32(i uint) int32
- func (bs *Bitstring) Int64(i uint) int64
- func (bs *Bitstring) Int8(i uint) int8
- func (bs *Bitstring) Intn(nbits, i uint) int32
- func (bs *Bitstring) Len() int
- func (bs *Bitstring) OnesCount() uint
- func (bs *Bitstring) SetBit(i uint)
- func (bs *Bitstring) SetInt16(i uint, x int16)
- func (bs *Bitstring) SetInt32(i uint, x int32)
- func (bs *Bitstring) SetInt64(i uint, x int64)
- func (bs *Bitstring) SetInt8(i uint, x int8)
- func (bs *Bitstring) SetIntn(n, i uint, x uint)
- func (bs *Bitstring) SetUint16(i uint, x uint16)
- func (bs *Bitstring) SetUint32(i uint, x uint32)
- func (bs *Bitstring) SetUint64(i uint, x uint64)
- func (bs *Bitstring) SetUint8(i uint, x uint8)
- func (bs *Bitstring) SetUintn(n, i uint, x uint)
- func (bs *Bitstring) String() string
- func (bs *Bitstring) Uint16(i uint) uint16
- func (bs *Bitstring) Uint32(i uint) uint32
- func (bs *Bitstring) Uint64(i uint) uint64
- func (bs *Bitstring) Uint8(i uint) uint8
- func (bs *Bitstring) Uintn(n, i uint) uint
- func (bs *Bitstring) ZeroesCount() uint
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 MakeFromString ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
ClearBit clears the bit at index i.
If i is greater than the bitstring length, ClearBit will panic.
func (*Bitstring) Equals ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
SetInt16 sets the 16 bits starting at i with the value of x. It panics if there are not enough bits.
func (*Bitstring) SetInt32 ¶
SetInt32 sets the 32 bits starting at i with the value of x. It panics if there are not enough bits.
func (*Bitstring) SetInt64 ¶
SetInt64 sets the 64 bits starting at i with the value of x. It panics if there are not enough bits.
func (*Bitstring) SetInt8 ¶
SetInt8 sets the 8 bits starting at i with the value of x. It panics if there are not enough bits.
func (*Bitstring) SetIntn ¶
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 ¶
SetUint16 sets the 16 bits starting at i with the value of x. It panics if there are not enough bits.
func (*Bitstring) SetUint32 ¶
SetUint32 sets the 32 bits starting at i with the value of x. It panics if there are not enough bits.
func (*Bitstring) SetUint64 ¶
SetUint64 sets the 64 bits starting at i with the value of x. It panics if there are not enough bits.
func (*Bitstring) SetUint8 ¶
SetUint8 sets the 8 bits starting at i with the value of x. It panics if there are not enough bits.
func (*Bitstring) SetUintn ¶
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) 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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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