bitarray

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2025 License: BSD-3-Clause Imports: 7 Imported by: 0

README ¶

bitarray

Go Reference

A bit array for Go.

Documentation ¶

Overview ¶

Package bitarray implements a bit array.

The least significant bit is at index 0. In the string representation used by Parse, MustParse, and BitArray.String it is the rightmost digit. The byte slice used by FromBytes and BitArray.ToBytes is in big-endian order.

Index ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

This section is empty.

Types ¶

type BitArray ¶

type BitArray struct {
	// contains filtered or unexported fields
}

BitArray type.

func Clone ¶

func Clone(ba *BitArray) *BitArray

Clone clones the BitArray.

func Concat ¶

func Concat(ba1, ba2 *BitArray) *BitArray

Concat returns a new BitArray with the bits from ba1 and ba2 concatenated.

func FromBytes ¶ added in v0.4.0

func FromBytes(bytes []byte) *BitArray

FromBytes creates a new BitArray from the byte slice. Panics if len(bytes) == 0.

func MustParse ¶

func MustParse(s string) *BitArray

MustParse creates a new BitArray by parsing the given string. Space characters are ignored. Panics if one of the characters in the string is not space, 0, or 1.

func New ¶

func New(size int, idx ...int) *BitArray

New creates a new BitArray with size bits and the bits at the given indexes set to 1. Panics if size <= 0 or one of the indexes is out of range.

func Parse ¶

func Parse(s string) (*BitArray, error)

Parse creates a new BitArray by parsing the given string. Space characters are ignored. Returns an error if one of the characters in the string is not space, 0, or 1.

func Slice ¶

func Slice(ba *BitArray, start, end int) *BitArray

Slice returns a new BitArray with the bits from ba at indexes [start, end).

func (*BitArray) And ¶

func (ba *BitArray) And(other *BitArray)

And sets ba = ba & other (bitwise AND).

func (*BitArray) AndNot ¶ added in v0.2.0

func (ba *BitArray) AndNot(other *BitArray)

AndNot sets ba = ba &^ other (bit clear).

func (*BitArray) Clear ¶

func (ba *BitArray) Clear()

Clear sets all bits to 0.

func (*BitArray) Count ¶

func (ba *BitArray) Count() int

Count returns the number of set bits.

func (*BitArray) Equal ¶

func (ba *BitArray) Equal(other *BitArray) bool

Equal reports whether the two bit arrays are equal.

func (*BitArray) Get ¶

func (ba *BitArray) Get(idx int) bool

Get reports whether the bit at index idx is set.

func (*BitArray) LeadingZeros ¶

func (ba *BitArray) LeadingZeros() int

LeadingZeros returns the number of leading unset bits.

func (*BitArray) MarshalBinary ¶

func (ba *BitArray) MarshalBinary() ([]byte, error)

MarshalBinary implements the encoding.BinaryMarshaler interface.

func (*BitArray) Not ¶

func (ba *BitArray) Not()

Not sets ba = ^ba.

func (*BitArray) Or ¶

func (ba *BitArray) Or(other *BitArray)

Or sets ba = ba | other (bitwise OR).

func (*BitArray) Reverse ¶ added in v0.5.0

func (ba *BitArray) Reverse()

Reverse reverses the bit order

func (*BitArray) Rotate ¶

func (ba *BitArray) Rotate(n int)

Rotate rotates the bit array by |n| bits. If n > 0 to the left, if n < 0 to the right.

func (*BitArray) Set ¶

func (ba *BitArray) Set(idx int)

Set sets the bit at index idx to 1.

func (*BitArray) SetAll ¶

func (ba *BitArray) SetAll()

SetAll sets all bits to 1.

func (*BitArray) Shift ¶

func (ba *BitArray) Shift(n int)

Shift shifts the bit array by |n| bits. If n > 0 to the left, if n < 0 to the right.

func (*BitArray) Size ¶

func (ba *BitArray) Size() int

Size returns the size of the bit array.

func (*BitArray) String ¶

func (ba *BitArray) String() string

String returns a string representation of the bit array.

func (*BitArray) ToBytes ¶ added in v0.4.0

func (ba *BitArray) ToBytes() []byte

ToBytes returns the bit array as a byte slice.

func (*BitArray) Toggle ¶ added in v0.3.0

func (ba *BitArray) Toggle(idx int) bool

Toggle toggles the state of the bit at index idx and reports whether it is set after being toggled.

func (*BitArray) TrailingZeros ¶

func (ba *BitArray) TrailingZeros() int

TrailingZeros returns the number of trailing unset bits.

func (*BitArray) UnmarshalBinary ¶

func (ba *BitArray) UnmarshalBinary(data []byte) error

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.

func (*BitArray) Unset ¶

func (ba *BitArray) Unset(idx int)

Unset sets the bit at index idx to 0.

func (*BitArray) Xor ¶

func (ba *BitArray) Xor(other *BitArray)

Xor sets ba = ba ^ other (bitwise XOR).

Jump to

Keyboard shortcuts

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