bits

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AmountInByte   = bits.UintSize / int(unsafe.Sizeof(uint(0)))
	AmountInUint64 = int(unsafe.Sizeof(uint64(0))) * AmountInByte
)

Variables

This section is empty.

Functions

func ErrInvalidRange

func ErrInvalidRange[
	BitStorage types.Unsigned,
	Value types.Integer,
](minVal, maxVal Value) error

func ErrRangeOutOfBounds

func ErrRangeOutOfBounds[Value types.Integer](minVal, maxVal Value) error

func ErrValueOutOfBounds

func ErrValueOutOfBounds[Value types.Integer](value Value) error

func ErrValuesMaskOutOfBounds

func ErrValuesMaskOutOfBounds[BitStorage types.Unsigned](valuesMask BitStorage) error

func FilterOutRange

func FilterOutRange[
	BitStorage types.Unsigned,
	Value types.Integer,
](
	allowed Seq[BitStorage],
	minVal, maxVal Value,
) (Seq[BitStorage], RangeState)

FilterOutRange transforms the given range of values to Seq; see NewFromRange. The given allowed bits are used as a clamp to filter out unfit values and to say via RangeState whether the given range is invalid or out of bounds.

func LeftmostBitPosIn

func LeftmostBitPosIn[BitStorage types.Unsigned]() int

LeftmostBitPosIn returns the leftmost bit position in BitStorage.

E.g., it is 7 for uint8, 15 for uint16, 31 for uint32, 63 for uint64.

Types

type RangeState

type RangeState struct {
	IsInvalid, IsOutOfBounds bool
}

type Seq

type Seq[BitStorage types.Unsigned] [1]BitStorage

Seq is a bit sequence stored in the value of the given BitStorage type.

NOTE: we cannot use a type parameter as right-hand side in type declaration so here is a trick: wrap it in the single-element array.

func FilterOut

func FilterOut[
	BitStorage types.Unsigned,
	Value types.Integer,
](allowed Seq[BitStorage], value Value) Seq[BitStorage]

FilterOut transforms the given value to Seq; see NewFromValue. The given allowed bits are used as a clamp to filter out unfit values.

func New

func New[BitStorage types.Unsigned](bs BitStorage) Seq[BitStorage]

New creates a new Seq from the given bits.

func NewFromRange

func NewFromRange[
	BitStorage types.Unsigned,
	Value types.Integer,
](minVal, maxVal Value) Seq[BitStorage]

NewFromRange takes two given values as [minVal; maxVal] range and transforms them into Seq.

E.g., [0; 0] and [1; 5] for uint8 BitStorage transform into 0b0000_0001 and 0b0011_1110 respectfully.

It returns a zero BitStorage if the range is invalid, that is:

  • minVal or maxVal is negative;
  • minVal is greater than maxVal;
  • maxVal is greater than LeftmostBitPosIn[BitStorage]().

func NewFromRangeOrError

func NewFromRangeOrError[
	BitStorage types.Unsigned,
	Value types.Integer,
](minVal, maxVal Value) (Seq[BitStorage], error)

NewFromRangeOrError is the same as NewFromRange but returns an error if the range is invalid.

func NewFromValue

func NewFromValue[
	BitStorage types.Unsigned,
	Value types.Integer,
](value Value) Seq[BitStorage]

NewFromValue takes the given value and transforms it into Seq.

The value is interpreted as a set bit position in BitStorage starting from the rightmost bit with position 0. If the value does not fit BitStorage positions range, that is, [0; LeftmostBitPosIn[BitStorage]()], then zero Seq will be returned.

func (Seq[BitStorage]) And

func (r Seq[BitStorage]) And(other Seq[BitStorage]) Seq[BitStorage]

And performs a bitwise AND operation on the given Seq's.

func (Seq[BitStorage]) IsZero

func (r Seq[BitStorage]) IsZero() bool

IsZero returns true if Seq does not contain any bits.

func (Seq[BitStorage]) LeftShift

func (r Seq[BitStorage]) LeftShift(n uint) Seq[BitStorage]

LeftShift performs a bitwise left shift operation on the given Seq.

func (Seq[BitStorage]) LeftmostOnePos

func (r Seq[BitStorage]) LeftmostOnePos() int

LeftmostOnePos returns the position the leftmost one bit in BitStorage, if any, otherwise, -1.

fmt.Println(bitmask.New[uint8](0b0010_0000).LeftmostOnePos() == 5) // true
fmt.Println(bitmask.NewFromValue[uint8](5).LeftmostOnePos() == 5) // true
fmt.Println(bitmask.NewFromValue[uint8](0).LeftmostOnePos() == -1) // true

func (Seq[BitStorage]) Not

func (r Seq[BitStorage]) Not() Seq[BitStorage]

Not performs a bitwise NOT operation on the given Seq.

func (Seq[BitStorage]) Or

func (r Seq[BitStorage]) Or(other Seq[BitStorage]) Seq[BitStorage]

Or performs a bitwise OR operation on the given Seq's.

func (Seq[BitStorage]) OrWithOverlapCheck

func (r Seq[BitStorage]) OrWithOverlapCheck(
	other Seq[BitStorage],
) (concatenated Seq[BitStorage], overlapped bool)

OrWithOverlapCheck performs a bitwise OR operation on the given Seq's and says whether they overlap.

func (Seq[BitStorage]) RightShift

func (r Seq[BitStorage]) RightShift(n uint) Seq[BitStorage]

RightShift performs a bitwise right shift operation on the given Seq.

func (Seq[BitStorage]) RightmostOnePos

func (r Seq[BitStorage]) RightmostOnePos() int

RightmostOnePos returns the position the rightmost one bit in BitStorage, if any, otherwise, AmountInUint64.

fmt.Println(bitmask.New[uint8](0b0000_0010).RightmostOnePos() == 1) // true
fmt.Println(bitmask.NewFromValue[uint8](1).RightmostOnePos() == 1) // true
fmt.Println(bitmask.NewFromValue[uint8](0).RightmostOnePos() == bitmask.AmountInUint64) // true

func (Seq[BitStorage]) Value

func (r Seq[BitStorage]) Value() BitStorage

Value returns the underlying bits.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL