Documentation ¶
Overview ¶
Package bitstring implements a fixed length bit string type and bit string manipulation functions
Index ¶
- func Copy(dst, src *Bitstring)
- func EqualRange(bs1, bs2 *Bitstring, start, length int) bool
- func SwapRange(bs1, bs2 *Bitstring, start, length int)
- type Bitstring
- func (bs *Bitstring) BigInt() *big.Int
- func (bs *Bitstring) Bit(i int) bool
- func (bs *Bitstring) ClearBit(i int)
- func (bs *Bitstring) ClearRange(start, length int)
- func (bs *Bitstring) Data() []uint64
- func (bs *Bitstring) Equals(other *Bitstring) bool
- func (bs *Bitstring) FlipBit(i int)
- func (bs *Bitstring) FlipRange(start, length int)
- func (bs *Bitstring) Gray16(i int) uint16
- func (bs *Bitstring) Gray32(i int) uint32
- func (bs *Bitstring) Gray64(i int) uint64
- func (bs *Bitstring) Gray8(i int) uint8
- func (bs *Bitstring) Grayn(nbits, i int) uint64
- func (bs *Bitstring) Int16(i int) int16
- func (bs *Bitstring) Int32(i int) int32
- func (bs *Bitstring) Int64(i int) int64
- func (bs *Bitstring) Int8(i int) int8
- func (bs *Bitstring) Intn(nbits, i int) int32
- func (bs *Bitstring) Len() int
- func (bs *Bitstring) OnesCount() int
- func (bs *Bitstring) SetBit(i int)
- func (bs *Bitstring) SetInt16(i int, x int16)
- func (bs *Bitstring) SetInt32(i int, x int32)
- func (bs *Bitstring) SetInt64(i int, x int64)
- func (bs *Bitstring) SetInt8(i int, x int8)
- func (bs *Bitstring) SetIntn(n, i int, x int64)
- func (bs *Bitstring) SetRange(start, length int)
- func (bs *Bitstring) SetUint16(i int, x uint16)
- func (bs *Bitstring) SetUint32(i int, x uint32)
- func (bs *Bitstring) SetUint64(i int, x uint64)
- func (bs *Bitstring) SetUint8(i int, x uint8)
- func (bs *Bitstring) SetUintn(n, i int, x uint64)
- func (bs *Bitstring) String() string
- func (bs *Bitstring) Uint16(i int) uint16
- func (bs *Bitstring) Uint32(i int) uint32
- func (bs *Bitstring) Uint64(i int) uint64
- func (bs *Bitstring) Uint8(i int) uint8
- func (bs *Bitstring) Uintn(n, i int) uint64
- func (bs *Bitstring) ZeroesCount() int
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Copy ¶
func Copy(dst, src *Bitstring)
Copy copies a source Bitstring into a destination Bitstring, shrinking or expanding it if necessary.
func EqualRange ¶
EqualRange compares a given range of bits between 2 bitstrings.
It's like Equals but only compares the [start, start+length) range. EqualRange returns false if this range is not defined on both bitstrings.
func SwapRange ¶
SwapRange swaps a range of bits between 2 bitstrings.
The range [start, start+length) must exist on both bitstrings or SwapRange has undefined behavior.
Example ¶
bs1, _ := NewFromString("111") bs2, _ := NewFromString("000") // Swap 2 bits from index 0 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 New ¶
New creates a bit string of the specified length (in bits) with all bits initially set to zero (off).
Example ¶
// Create a 32-bit Bitstring bs := New(32) // upon creation all bits are unset fmt.Println(bs)
Output: 00000000000000000000000000000000
func NewFromString ¶
NewFromString returns the corresponding Bitstring for the given string of 1s and 0s in big endian order.
Example ¶
// Create a Bitstring from a string made of 0's and 1's. bs, _ := NewFromString("101001") fmt.Println(bs) fmt.Println(bs.Len(), "bits")
Output: 101001 6 bits
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 ¶
bs, _ := NewFromString("100") bi := bs.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.
func (*Bitstring) ClearBit ¶
ClearBit clears the bit at index i.
If i is greater than the bitstring length, ClearBit will panic.
Example ¶
bs := New(8) bs.SetBit(2) fmt.Println(bs) bs.ClearBit(2) fmt.Println(bs)
Output: 00000100 00000000
func (*Bitstring) ClearRange ¶
ClearRange clears a range of bits (sets all bits to 0).
The range [start, start+length) must exist or ClearRange has undefined behavior.
Example ¶
bs, _ := NewFromString("10101010") // Clear the 3 bits at offset 2. bs.ClearRange(2, 3) fmt.Println(bs)
Output: 10100010
func (*Bitstring) Equals ¶
Equals returns true if bs and other have the same length 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 ¶
bs := New(8) bs.FlipBit(2) fmt.Println(bs)
Output: 00000100
func (*Bitstring) FlipRange ¶
FlipRange flips a range of bits (flips the value of every bit).
The range [start, start+length) must exist or FlipRange has undefined behavior.
Example ¶
bs, _ := NewFromString("10101010") // Flip the 3 bits at offset 2. bs.FlipRange(2, 3) fmt.Println(bs)
Output: 10110110
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) OnesCount ¶
OnesCount counts the number of one bits.
Example ¶
bitstring := New(8) 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 ¶
bs := New(8) bs.SetBit(2) fmt.Println("bit 2:", bs.Bit(2)) fmt.Println("bit 7:", bs.Bit(7))
Output: bit 2: true bit 7: false
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 64.
func (*Bitstring) SetRange ¶
SetRange sets a range of bits (sets all bits to 1).
The range [start, start+length) must exist or SetBitRange has undefined behavior.
Example ¶
bs, _ := NewFromString("10101010") // Set the 3 bits at offset 2. bs.SetRange(2, 3) fmt.Println(bs)
Output: 10111110
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 ¶
bs := New(8) fmt.Println(bs.ZeroesCount())
Output: 8