bit

package
v1.2.23 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BigEndianCopy

func BigEndianCopy(src []byte, srcOffset uint, dst []byte, dstOffset uint, nbits uint)

BigEndianCopy copies n bits starting a given bit offset from a given byte array source into a given destination (at a given offset) assuming a little endian layout of bytes. For example, consider the array [0x90,0x7] which is [0b10010000,0b00000111]. Then, the bit offsets can be viewed as follows:

+---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+ | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | +---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+ | 00| 01| 02| 03| 04| 05| 06| 07| | 08| 09| 10| 11| 12| 13| 14| 15|

Now, consider copying 8 bits starting at offset 3. This represents the following bits:

+---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+ | | | | X | X | X | X | X | | X | X | X | | | | | | +---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+ | 00| 01| 02| 03| 04| 05| 06| 07| | 08| 09| 10| 11| 12| 13| 14| 15|

As such, we see how the big end treatment of bytes impacts the bits which are copied.

func BigEndianRead

func BigEndianRead(src []byte, bitoffset uint) bool

BigEndianRead reads the bit at a given bit offset out of an array of bytes arranged in big endian format. So, for example, reading bit 0 from the byte 0b0111_11111 returns 0, but reading bit 7 returns 1.

func BigEndianWrite

func BigEndianWrite(val bool, src []byte, bitoffset uint)

BigEndianWrite writes a bit to a given bit offset in an array of bytes arranged in big endian format. So, for example, writing 1 at offset 15 into an array [0x00,0x00] yields [0x00,0x01].

func BytesRequiredFor

func BytesRequiredFor(bitwidth uint) uint

BytesRequiredFor returns the minimum number of bytes required to hold the given bitwidth. For example, the number of bytes to hold a u16 is 2 bytes, whilst the minimum required to hold a u17 is 3 bytes.

func DecodeArray

func DecodeArray[T any](bitwidth uint, bytes []byte, decoder func([]byte) T) ([]T, uint)

DecodeArray decodes the given set of bytes into an array of arbitrary fixed-width values. Observe that values are assumed to be packed tightly (i.e. without any padding). Consider the following input byte array:

| 00 | 01 | 02 | 03 | +------+------+------+------+ | 0x31 | 0xf0 | 0x0e | 0x1d |

Then, decoding this into a u4 array will produce the following:

| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | +-----+-----+-----+-----+-----+-----+-----+-----+ | 0x3 | 0x1 | 0xf | 0x0 | 0x0 | 0xe | 0x1 | 0xd |

Finally, the number of unused (i.e. remaining) bits is returned (which will be zero if (len(bytes)*8)%bitwidth == 0).

NOTE: the array parsed into the decoder function is reused across different elements and, hence, the decoder function should clone it if necessary.

func LittleEndianCopy

func LittleEndianCopy(src []byte, srcOffset uint, dst []byte, dstOffset uint, nbits uint)

LittleEndianCopy copies n bits starting a given bit offset from a given byte array source into a given destination (at a given offset) assuming a little endian layout of bytes. For example, consider the array [0x90,0x7] which is [0b10010000,0b00000111]. Then, the bit offsets can be viewed as follows:

+---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+ | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | +---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+ | 07| 06| 05| 04| 03| 02| 01| 00| | 15| 14| 13| 12| 11| 10| 09| 08|

Now, consider copying 8 bits starting at offset 3. This represents the following bits:

+---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+ | X | X | X | X | X | | | | | | | | | | X | X | X | +---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+ | 07| 06| 05| 04| 03| 02| 01| 00| | 15| 14| 13| 12| 11| 10| 09| 08|

As such, we see how the little end treatment of bytes impacts the bits which are copied.

func LittleEndianRead

func LittleEndianRead(src []byte, bitoffset uint) bool

LittleEndianRead reads the bit at a given bit offset out of an array of bytes arranged in little endian format. So, for example, reading bit 0 from the byte 0b0111_11111 returns 1, but reading bit 7 returns 0.

func LittleEndianWrite

func LittleEndianWrite(val bool, src []byte, bitoffset uint)

LittleEndianWrite writes a bit to a given bit offset in an array of bytes arranged in little endian format. So, for example, writing 1 at offset 15 into an array [0x00,0x00] yields [0x00,0x80].

func NewBuffer

func NewBuffer(bitwidth uint) []byte

NewBuffer allocates a byte array which is large enough to hold values of the given bitwidth, as needed for the Reader.

func Width

func Width(bound uint) uint

Width determines the smallest bitwidth which can hold all values below a given bound. Basically, the bound is raised to the nearest power of 2. For example, given 4 this should return 2bits, whilst 5 should return 3bits, etc.

Types

type Predicate

type Predicate[T any] = func(T) bool

Predicate abstracts the notion of a function which identifies something.

type Reader

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

Reader provides a mechanism for reading bits from a given array of bytes, where the least significant bits are read first. For example, consider sequence of bytes [0x9f,0x05] can be views as the following bit sequence:

| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 || 8 | 9 | A | B | C | D | E | F | +===+===+===+===+===+===+===+===++===+===+===+===+===+===+===+===+ | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 1 || 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | | | | | | | 1 | 1 | 1 | 1 | 1 | 0 | 0 |

The above illustrates the outcome from reading 7 bits. In such case, the value 0b0011111 is written into the target buffer.

func NewReader

func NewReader(bytes []byte) Reader

NewReader constructs a new bit reader.

func (*Reader) BigEndianReadInto

func (p *Reader) BigEndianReadInto(nbits uint, buf []byte) uint

BigEndianReadInto reads the n least significant bits from the underlying array into a given target array, returning the total number of bytes affected. This assume a big endian encoding of bytes.

func (*Reader) LittleEndianReadInto

func (p *Reader) LittleEndianReadInto(nbits uint, buf []byte) uint

LittleEndianReadInto reads the n least significant bits from the underlying array into a given target array, returning the total number of bytes affected. This assume a little endian encoding of bytes.

func (*Reader) Remaining

func (p *Reader) Remaining() uint

Remaining returns the remaining number of bits which can be read.

type Set

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

Set provides a straightforward bitset implementation. That is, a set of (unsigned) integer values implemented as an array of bits.

func NewSet

func NewSet(size uint) *Set

NewSet creates a Set of the given size.

func (*Set) Clone

func (p *Set) Clone() Set

Clone creates a true copy of this bitset which ensures no aliasing between this set and the result.

func (*Set) Contains

func (p *Set) Contains(val uint) bool

Contains checks whether a given value is contained, or not.

func (*Set) Count

func (p *Set) Count() uint

Count returns the number of bits in the bitset which are set to one.

func (*Set) Get

func (p *Set) Get(i uint) bool

Get the value of the iᵗʰ bit

func (*Set) Insert

func (p *Set) Insert(val uint)

Insert a given value into this set.

func (*Set) InsertAll

func (p *Set) InsertAll(vals ...uint)

InsertAll inserts zero or more elements into this bitset.

func (*Set) Iter

func (p *Set) Iter() iter.Iterator[uint]

Iter returns an iterator over the elements of this bitset.

func (*Set) Remove

func (p *Set) Remove(val uint)

Remove a given value from this set.

func (*Set) Set

func (p *Set) Set(i uint, v bool)

Set the iᵗʰ bit to v

func (*Set) String

func (p *Set) String() string

func (*Set) Union

func (p *Set) Union(bits Set) bool

Union inserts all elements from a given bitset into this bitset, return true if there is some change.

Jump to

Keyboard shortcuts

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