| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
This package is not in the latest version of its module.
Go to latest Published: May 20, 2021 License: MITThe Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Modules with tagged versions give importers more predictable builds.
When a project reaches major version v1 it is considered stable.
Package bitstring implements a fixed length bit string type and bit string manipulation functions
This section is empty.
This section is empty.
func Copy(dst, src *Bitstring)
Copy copies a source Bitstring into a destination Bitstring, shrinking or expanding it if necessary.
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.
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
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.
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
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
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.
BigInt returns the big.Int representation of bs.
Example ¶
bs, _ := NewFromString("100")
bi := bs.BigInt()
fmt.Println(bi.Int64())
Output: 4
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.
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
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
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).
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
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
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.
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.
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.
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.
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.
Int8 returns the int8 value represented by the 8 bits starting at the given bit. It panics if there are not enough bits.
Int16 returns the int16 value represented by the 16 bits starting at the given bit. It panics if there are not enough bits.
Int32 returns the int32 value represented by the 32 bits starting at the given bit. It panics if there are not enough bits.
Int64 returns the int64 value represented by the 64 bits starting at the given bit. It panics if there are not enough bits.
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.
OnesCount counts the number of one bits.
Example ¶bitstring := New(8) fmt.Println(bitstring.OnesCount())
Output: 0
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
SetInt8 sets the 8 bits starting at i with the value of x. It panics if there are not enough bits.
SetInt16 sets the 16 bits starting at i with the value of x. It panics if there are not enough bits.
SetInt32 sets the 32 bits starting at i with the value of x. It panics if there are not enough bits.
SetInt64 sets the 64 bits starting at i with the value of x. It panics if there are not enough bits.
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.
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
SetUint8 sets the 8 bits starting at i with the value of x. It panics if there are not enough bits.
SetUint16 sets the 16 bits starting at i with the value of x. It panics if there are not enough bits.
SetUint32 sets the 32 bits starting at i with the value of x. It panics if there are not enough bits.
SetUint64 sets the 64 bits starting at i with the value of x. It panics if there are not enough bits.
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.
Uint8 returns the uint8 value represented by the 8 bits starting at the given bit. It panics if there are not enough bits.
Uint16 returns the uint16 value represented by the 16 bits starting at the given bit. It panics if there are not enough bits.
Uint32 returns the uint32 value represented by the 32 bits starting at the given bit. It panics if there are not enough bits.
Uint64 returns the uint64 value represented by the 64 bits starting at the given bit. It panics if there are not enough bits.
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
ZeroesCount counts the number of zero bits.
Example ¶bs := New(8) fmt.Println(bs.ZeroesCount())
Output: 8
| Back | FazBrowse Home | New Git URL |