Skip to content

Package bitop implements simple functions to manipulate bits, mimicking package strings.

License

Notifications You must be signed in to change notification settings

yulin-physics/bitop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bit Operations in Go

This bit operations library mimicks the functions of Go strings library.

For learning purposes only, use at your own risk.

Index

Types:

Unit

Functions:

ClearFromRight Contains ColumnJoin Flip FlipAtIndex GetBitAtIndex IsPalindrome Join LastIndex RemoveBit Repeat Replace Reverse SplitAt TruncateFromLeft TruncateFromRight

Types

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
}

Functions

func Contains

func Contains(b, sub Unit) bool

Checks the target binary b has at least one part that matches the sub-binary value.

func LastIndex

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

func GetBitAtIndex(b Unit, ind int) uint

Finds the bit at index ind of binary b.

func SplitAt

func SplitAt(b Unit, ind int) []uint

Splits the binary in two at the index, returns the 2 sub-binaries.

func Join

func Join(bs []Unit, sep Unit) uint

Join combines the binary values into one, separated by the given delimiter.

func ColumnJoin

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

func TruncateFromRight(b uint, pos int) uint

TruncateFromRight trims off bits from right, up to but not including the index.

func TruncateFromLeft

func TruncateFromLeft(b Unit, ind int) uint

TruncateFromLeft trims off bits from left, up to but not including the index.

func ClearFromRight

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

func RemoveBit(b Unit, ind int) uint

RemoveBit removes the bit at the index from the binary.

func Repeat

func Repeat(b Unit, count int) uint

Repeat constructs a binary based on a given repeating bit pattern.

func Replace

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

func Flip(b Unit) uint

Flips all bits.

func FlipAtIndex

func FlipAtIndex(b Unit, ind int) uint

Flips the bit at index ind in the binary.

func Reverse

func Reverse(b Unit) uint

Returns bits in reversed order.

func IsPalindrome

func IsPalindrome(b Unit) bool

Returns true if the binary contains symmetry.

About

Package bitop implements simple functions to manipulate bits, mimicking package strings.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages