Documentation
¶
Overview ¶
Package bitfield64 is a simple, quick stack-based bit-field manipulator package of 64 bits (or less) in length. If you need more bits you either need to create an array of bitfield64-s (and stay on the stack) or need to switch to the heap-based bitfield package.
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.
Index ¶
- 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 BitField64 ¶
type BitField64 uint64
BitField64 type utilizing the power of 64bit CPUs. It lives on the stack
func New ¶
func New() BitField64
New returns a zeroed (all false) bit-field that can store size elements
func (BitField64) And ¶
func (bf64 BitField64) And(bfo BitField64) BitField64
And returns the binary AND of the two bitfields
func (BitField64) Clear ¶
func (bf64 BitField64) Clear(pos int) BitField64
Clear clears the bit at position pos
func (BitField64) ClearAll ¶
func (bf64 BitField64) ClearAll() BitField64
ClearAll returns a bitfield where all 64 bits are set
func (BitField64) ClearMul ¶ added in v1.3.0
func (bf64 BitField64) ClearMul(pos ...int) BitField64
ClearMul sets the bits at position pos
func (BitField64) Flip ¶ added in v1.1.0
func (bf64 BitField64) Flip(pos int) BitField64
Flip inverts the bit at position pos
Example ¶
a := New().SetAll().Flip(0) fmt.Println(a.String())
Output: 0111111111111111111111111111111111111111111111111111111111111111
func (BitField64) Get ¶
func (bf64 BitField64) Get(pos int) bool
Get returns true if bit at position pos is set, false otherwise
func (BitField64) Left ¶ added in v1.2.0
func (bf64 BitField64) Left(count int) BitField64
Left returns leftmost count bits: [0, count-1]
func (BitField64) Mid ¶ added in v1.2.0
func (bf64 BitField64) Mid(pos, count int) BitField64
Mid returns count bits from position pos
func (BitField64) Not ¶
func (bf64 BitField64) Not() BitField64
Not returns the bitfield with each bit inverted: 0 becomes 1, 1 becomes 0
func (BitField64) OnesCount ¶
func (bf64 BitField64) OnesCount() int
OnesCount returns the number of bits set
func (BitField64) Or ¶
func (bf64 BitField64) Or(bfo BitField64) BitField64
Or returns the binary OR of the two bitfields
func (BitField64) Right ¶ added in v1.2.0
func (bf64 BitField64) Right(count int) BitField64
Right returns rightmost count bits [63-count, 63]
func (BitField64) Rotate ¶ added in v1.2.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 ¶
func (bf64 BitField64) Set(pos int) BitField64
Set sets the bit at position pos
func (BitField64) SetAll ¶
func (bf64 BitField64) SetAll() BitField64
SetAll returns a bitfield where all 64 bits are set
func (BitField64) SetMul ¶ added in v1.3.0
func (bf64 BitField64) SetMul(pos ...int) BitField64
SetMul sets the bits at position pos
Example ¶
a := New().SetMul(2, 4) fmt.Println(a.StringPretty())
Output: 00101
func (BitField64) Shift ¶ added in v1.2.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 := New().SetAll().Shift(3).Left(5) fmt.Println(a.StringPretty())
Output: 00011
func (BitField64) Shift2 ¶ added in v1.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 v1.4.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 v1.4.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 ¶
func (bf64 BitField64) Xor(bfo BitField64) BitField64
Xor returns the binary XOR of the two bitfields
Example ¶
a := New().SetMul(0, 3) b := New().Set(1) fmt.Println(a.Xor(b).StringPretty())
Output: 1101