Documentation
¶
Overview ¶
Package bitfield is for storing and manipulating bit-data.
There are two structs defined: BitField64 is in case you want to store 64 or less bits, while BitField is for storing arbitrary number of bits.
Index ¶
- type BitField
- func (bf *BitField) And(bfOther *BitField) *BitField
- func (bf *BitField) Append(other *BitField) *BitField
- func (bf *BitField) Clear(pos ...int) *BitField
- func (bf *BitField) ClearAll() *BitField
- func (bf *BitField) Clone() *BitField
- func (bf *BitField) Copy(dest *BitField) bool
- func (bf *BitField) Equal(bfOther *BitField) bool
- func (bf *BitField) Flip(pos ...int) *BitField
- func (bf *BitField) Get(pos int) bool
- func (bf *BitField) Left(count int) *BitField
- func (bf *BitField) Len() int
- func (bf *BitField) Mid(pos, count int) *BitField
- func (bf *BitField) Mut() *BitField
- func (bf *BitField) Not() *BitField
- func (bf *BitField) OnesCount() int
- func (bf *BitField) Or(bfOther *BitField) *BitField
- func (bf *BitField) Resize(newLen int) *BitField
- func (bf *BitField) Right(count int) *BitField
- func (bf *BitField) Rotate(amount int) *BitField
- func (bf *BitField) Set(pos ...int) *BitField
- func (bf *BitField) SetAll() *BitField
- func (bf *BitField) Shift(count int) *BitField
- func (bf *BitField) String() string
- func (bf *BitField) Xor(bfOther *BitField) *BitField
- type BitField64
- func (bf64 BitField64) And(bfo BitField64) BitField64
- func (bf64 BitField64) Clear(pos int) BitField64
- func (bf64 BitField64) ClearAll() BitField64
- func (bf64 BitField64) ClearMul(pos ...int) BitField64
- func (bf64 BitField64) Flip(pos int) BitField64
- func (bf64 BitField64) Get(pos int) bool
- func (bf64 BitField64) Left(count int) BitField64
- func (bf64 BitField64) Mid(pos, count int) BitField64
- func (bf64 BitField64) Not() BitField64
- func (bf64 BitField64) OnesCount() int
- func (bf64 BitField64) Or(bfo BitField64) BitField64
- func (bf64 BitField64) Right(count int) BitField64
- func (bf64 BitField64) Rotate(count int) BitField64
- func (bf64 BitField64) Set(pos int) BitField64
- func (bf64 BitField64) SetAll() BitField64
- func (bf64 BitField64) SetMul(pos ...int) BitField64
- func (bf64 BitField64) Shift(count int) BitField64
- func (bf64 BitField64) Shift2(count int) (ret, discarded BitField64)
- func (bf64 BitField64) String() string
- func (bf64 BitField64) StringPretty() string
- func (bf64 BitField64) Xor(bfo BitField64) BitField64
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BitField ¶
type BitField struct {
// contains filtered or unexported fields
}
BitField is a flexible size version of BitField64.
Most functions are chainable, positions outside the [0,len) range will get the modulo treatment, so Get(len) will return the 0th bit, Get(-1) will return the last bit: Get(len-1)
Most methods do not modify the underlying bitfield but create a new and return that. You can change this behaviour by calling .Mut() method. In this case all methods explicitely marked as 'Mutable.' will be modified in-place. This reduces allocations (for cases where speed does matter).
func NewBitField ¶ added in v2.1.0
NewBitField creates a new BitField of length len and returns it. Panics if len<0
func (*BitField) Append ¶
Append appends 'other' BitField to the end A newly created bitfield will be returned
func (*BitField) Clear ¶
Clear clears the bit(s) at position pos. Mutable.
Example ¶
bf := NewBitField(4).SetAll().Clear(0, -1) fmt.Println(bf)
Output: 0110
func (*BitField) Copy ¶
Copy copies the content of BitField bf to dest. Returns false if the two bitfields differ in size, true otherwise
func (*BitField) Left ¶
Left returns count bits in the range of [0,count-1] as a new BitField Panics if count<0
func (*BitField) Mid ¶
Mid returns counts bits from position pos as a new BitField Panics if count<0
func (*BitField) Mut ¶
Mut sets the mutable flag. This can reduce number of copying if execution time is important. Methods where description contains 'Mutable.' will modify content in-place.
Example ¶
bf := NewBitField(4).Set(0) bf.Set(1) // this is set then discarded! fmt.Println("without Mut():", bf) bf.Mut().Set(1) fmt.Println("with Mut():", bf)
Output: without Mut(): 1000 with Mut(): 1100
func (*BitField) Resize ¶
Resize resizes the bitfield to newLen in size. Panics is newLen<0 Returns a newly allocated one, leaves the original intact. If newLen < Len() bits are lost at the end. If newLen > Len() the newly added bits will be zeroed. Mutable.
func (*BitField) Right ¶
Right returns count bits in the range of [63-count,63] as a new BitField Panics if count<0
func (*BitField) Rotate ¶
Rotate rotates by amount bits and returns it If amount>0 it rotates towards higher bit positions, otherwise it rotates towards lower bit positions. Mutable.
func (*BitField) Shift ¶
Shift shifts thes bitfield by count bits and returns it. If count is positive it shifts towards higher bit positions; If negative it shifts towards lower bit positions. Bits exiting at one end are discarded; bits entering at the other end are zeroed. Mutable.
Example (E1) ¶
bf := NewBitField(3).Set(0).Shift(1) fmt.Println(bf)
Output: 010
Example (E2) ¶
bf := NewBitField(3).Set(0).Shift(3) fmt.Println(bf)
Output: 000
type BitField64 ¶ added in v2.3.0
type BitField64 uint64
BitField64 is a simple, quick, stack-based bit-field manipulator for 64 bits (or less) in length.
Methods are stateless and free from side-effects.
It was designed to be chainable. Range for position must be [0, 63]. position outside this range will get the modulo treatment, so 64 will point to the 0th element, -1 will address the last element (i.e. 63rd), -2 the one before (i.e. 62nd), etc.
func New64 ¶ added in v2.3.0
func New64() BitField64
New64 returns a zeroed (all false) bit-field that can store 64 elements
func (BitField64) And ¶ added in v2.3.0
func (bf64 BitField64) And(bfo BitField64) BitField64
And returns the binary AND of the two bitfields
func (BitField64) Clear ¶ added in v2.3.0
func (bf64 BitField64) Clear(pos int) BitField64
Clear clears the bit at position pos
func (BitField64) ClearAll ¶ added in v2.3.0
func (bf64 BitField64) ClearAll() BitField64
ClearAll returns a bitfield where all 64 bits are set
func (BitField64) ClearMul ¶ added in v2.3.0
func (bf64 BitField64) ClearMul(pos ...int) BitField64
ClearMul sets the bits at position pos
func (BitField64) Flip ¶ added in v2.3.0
func (bf64 BitField64) Flip(pos int) BitField64
Flip inverts the bit at position pos
Example ¶
a := New64().SetAll().Flip(0) fmt.Println(a.String())
Output: 0111111111111111111111111111111111111111111111111111111111111111
func (BitField64) Get ¶ added in v2.3.0
func (bf64 BitField64) Get(pos int) bool
Get returns true if bit at position pos is set, false otherwise
func (BitField64) Left ¶ added in v2.3.0
func (bf64 BitField64) Left(count int) BitField64
Left returns leftmost count bits: [0, count-1]
func (BitField64) Mid ¶ added in v2.3.0
func (bf64 BitField64) Mid(pos, count int) BitField64
Mid returns count bits from position pos
func (BitField64) Not ¶ added in v2.3.0
func (bf64 BitField64) Not() BitField64
Not returns the bitfield with each bit inverted: 0 becomes 1, 1 becomes 0
func (BitField64) OnesCount ¶ added in v2.3.0
func (bf64 BitField64) OnesCount() int
OnesCount returns the number of bits set
func (BitField64) Or ¶ added in v2.3.0
func (bf64 BitField64) Or(bfo BitField64) BitField64
Or returns the binary OR of the two bitfields
func (BitField64) Right ¶ added in v2.3.0
func (bf64 BitField64) Right(count int) BitField64
Right returns rightmost count bits [63-count, 63]
func (BitField64) Rotate ¶ added in v2.3.0
func (bf64 BitField64) Rotate(count int) BitField64
Rotate rotates by count bits: Bits exiting at one end entering at the other end. If count is positive it rotates towards higher positions; If negative it rotates towards lower positions.
func (BitField64) Set ¶ added in v2.3.0
func (bf64 BitField64) Set(pos int) BitField64
Set sets the bit at position pos
func (BitField64) SetAll ¶ added in v2.3.0
func (bf64 BitField64) SetAll() BitField64
SetAll returns a bitfield where all 64 bits are set
func (BitField64) SetMul ¶ added in v2.3.0
func (bf64 BitField64) SetMul(pos ...int) BitField64
SetMul sets the bits at position pos
Example ¶
a := New64().SetMul(2, 4) fmt.Println(a.StringPretty())
Output: 00101
func (BitField64) Shift ¶ added in v2.3.0
func (bf64 BitField64) Shift(count int) BitField64
Shift shift bits by count positions. Bits exiting at one end are discarded; bits entering at the other end are zeroed. If count is positive it shifts towards higher positions; If negative it shifts towards lower positions.
Example ¶
// SetAll(): all bits are 1 // Shift(3): 3 zeroes enter from left // Left(5): takes first 3 zero bits and two 1s. a := New64().SetAll().Shift(3).Left(5) fmt.Println(a.StringPretty())
Output: 00011
func (BitField64) Shift2 ¶ added in v2.3.0
func (bf64 BitField64) Shift2(count int) (ret, discarded BitField64)
Shift2 is same as Shift but it returns the discarded bits as well
func (BitField64) String ¶ added in v2.3.0
func (bf64 BitField64) String() string
String returns the bit-representation of the bitfield Unlike in number to binary conversion here everything is reversed as position grows from 0 to 63, left to right So e.g. New().Set(2) print 0010000000000000000000000000000000000000000000000000000000000000
func (BitField64) StringPretty ¶ added in v2.3.0
func (bf64 BitField64) StringPretty() string
StringPretty converts the bitfield to a string, but tailing zeros are dropped. So e.g. New().Set(2) will print "001"
func (BitField64) Xor ¶ added in v2.3.0
func (bf64 BitField64) Xor(bfo BitField64) BitField64
Xor returns the binary XOR of the two bitfields
Example ¶
a := New64().SetMul(0, 3) b := New64().Set(1) fmt.Println(a.Xor(b).StringPretty())
Output: 1101