This bit operations library mimicks the functions of Go strings library.
For learning purposes only, use at your own risk.
Types:
Functions:
ClearFromRight Contains ColumnJoin Flip FlipAtIndex GetBitAtIndex IsPalindrome Join LastIndex RemoveBit Repeat Replace Reverse SplitAt TruncateFromLeft TruncateFromRight
The visible length of the binary is required in bit shifting in this package, therefore a new type Unit
is used.
An Unit composed of the binary itself and length of the binary.
type Unit struct {
value uint
leng int
}
func Contains(b, sub Unit) bool
Checks the target binary b
has at least one part that matches the sub-binary value.
func LastIndex(b, sub Unit) int
Finds the index (counting from left to right) of the last bit pattern in b
that matches sub
.
func GetBitAtIndex(b Unit, ind int) uint
Finds the bit at index ind
of binary b
.
func SplitAt(b Unit, ind int) []uint
Splits the binary in two at the index, returns the 2 sub-binaries.
func Join(bs []Unit, sep Unit) uint
Join combines the binary values into one, separated by the given delimiter.
func ColumnJoin(rows []uint, colLeng int) []uint
ColumnJoin combines the binary values in each corresponding bit position, forming array of columns.
colLeng is usually the bit length of an element in rows, but since leading zeroes are ommited and in case of variable length binaries in input, user needs to specify the bit length.
func TruncateFromRight(b uint, pos int) uint
TruncateFromRight trims off bits from right, up to but not including the index.
func TruncateFromLeft(b Unit, ind int) uint
TruncateFromLeft trims off bits from left, up to but not including the index.
func ClearFromRight(b Unit, ind int) uint
Clear preserves the binary length and resets the bits from right to zero, up to but not including the index.
func RemoveBit(b Unit, ind int) uint
RemoveBit removes the bit at the index from the binary.
func Repeat(b Unit, count int) uint
Repeat constructs a binary based on a given repeating bit pattern.
func Replace(b Unit, old Unit, new Unit, n int) uint
Replace constructs a new binary with the old sub bits replaced by the new up to n times.
func Flip(b Unit) uint
Flips all bits.
func FlipAtIndex(b Unit, ind int) uint
Flips the bit at index ind
in the binary.
func Reverse(b Unit) uint
Returns bits in reversed order.
func IsPalindrome(b Unit) bool
Returns true if the binary contains symmetry.