bitvector package

The bitvector package contains modules for handling bit vectors and their optimizations.

Modules

BitVector

class bytemaker.bitvector.bitvector.BitVector(source: BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray | int | None = None, buffer: Buffer | None = None, *args, **kwargs)[source]

Bases: bitarray, MutableSequence[Literal[0, 1]]

A mutable sequence of bits.

This class’s behavior is largely a superset of that of bytearray.

excepting certain methods that are not applicable to bits.

This particular implementation is currently a

subclass of bitarray, which provides C-level performance for bit manipulation.

However, only the behavior documented here (that is, that does not stem

from the bitarray superclass) is guaranteed

append(value: int) None[source]

Appends the provided bit to end (right) of the BitVector.

Parameters:

value (int) – The bit to append

bin(sep: str | None = None, bytes_per_sep: int = 1) str[source]

Convert the BitVector to a binary string prefixed by 0x. If sep is not None, the string is split into chunks of bytes_per_sep bytes

punctuated by sep.

Parameters:
  • sep (Optional[str]) – The separator to use

  • bytes_per_sep (int) – The number of bytes per separator

Returns:

The BitVector converted to a binary string

Return type:

str

classmethod cast_if_not_bitarray(obj: BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray) Self | BitVector[source]

Casts the object to a BitVector if it is not already a BitVector.

Parameters:
  • cls (type[Self]) – The class of the object

  • obj (BitsConstructible) – The object to cast

Returns:

The object as a BitVector

Return type:

Union[Self, BitVector]

clear() None[source]

Removes all bits from the BitVector.

copy() Self[source]

Returns a shallow copy of the BitVector

count(value: BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray | int, start: int = 0, end: int | None = None) int[source]
Counts the occurrences of the given bit in the BitVector

between the given start (inclusive) and end (exclusive) indices.

Parameters:
  • value (Union[BitsConstructible, int]) – The bit to count

  • start (int, optional) – The start index. Defaults to 0.

  • end (Optional[int], optional) – The end index. Defaults to None.

Returns:

The number of occurrences of the bit

(within the provided index range, if any)

Return type:

int

endswith(substrings: BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray | Literal[0, 1] | Iterable[BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray], start: int = 0, stop: int | None = None) bool[source]

Checks if the bitarray ends with the given substring. If start and stop are provided, the check is performed only

on the bits between the start (inclusive) and stop exclusive) indices.

Parameters:
  • substrings (Union[BitsConstructible, BitVector, Literal[0, 1], Iterable[Union[BitsConstructible, BitVector]]]) – The substring(s) to check

  • start (int, optional) – The start index. Defaults to 0.

  • stop (Optional[int], optional) – The stop index. Defaults to None.

extend(values: BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray) None[source]
Extends the BitVector with the provided bits (appends them

on the right).

Parameters:

values (BitsConstructible) – The bits to append

find(value: BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray | int, start: int = 0, stop: int | None = None) int[source]

Finds the first occurrence of the given bit in the BitVector. If the bit is not found, -1 is returned.

Parameters:
  • value (Union[BitsConstructible, int]) – The bit to find

  • start (int, optional) – The initial index to search. Defaults to 0.

  • stop (Optional[int], optional) – The index to give up on. Defaults to None.

Returns:

The index of the first occurrence of the bit, or -1 if not found

Return type:

int

classmethod from01(string: str) Self[source]

Create a BitVector from a binary string. The string may contain any of ‘_’, ‘-’, ‘ ‘, or ‘:’. The string must contain only 0s and 1s other than these.

Parameters:

string (str) – The binary string to convert

Returns:

The BitVector created from the binary string

Return type:

BitVector

classmethod from_bytes(byte_arr: bytes, reverse_endianness=False)[source]
classmethod from_chararray(char_array: str, encoding: str | dict[str, Union[bytemaker.bitvector.bitvector_with_bitarray_speedup.BitVector, bytes, str, collections.abc.Iterable[Literal[0, 1]], bytemaker.bitvector.bitvector_with_bitarray_speedup.BitsCastable, bitarray.bitarray]] = 'utf-8') Self[source]

Create a BitVector from a char_array string where each character is a byte. The string is encoded using the given encoding. If this is a standard

byte encoding, str.encode is used to convert the string to bytes.

Otherwise, the string is converted to bytes using the given mapping,

iterating over the char_array and building up substrings until a substring is found in the encoding mapping to convert to a BitVector. These converted BitVectors are concatenated together to form the final returned BitVector.

Parameters:
  • char_array (str) – The string to convert

  • encoding (Union[str, dict[str, BitsConstructible]]) – The encoding to use

Returns:

The BitVector created from the string

Return type:

BitVector

classmethod from_int(integer: int, size=None)[source]
Converts an integer to a Bits object.

The size parameter determines the number of bits to use. If size is not provided, the number of bits required to represent the integer is used.

classmethod frombase(string: str, base: int) Self[source]

Create a BitVector from a string in a given base. The string may contain any of ‘_’, ‘-’, ‘ ‘, or ‘:’. In the case of bases 2, 8, and 16,

the string may start with “0b”, “0o”, or “0x” respectively.

Parameters:
  • string (str) – The string to convert

  • base (int) – The base of the string

Returns:

The BitVector created from the string

Return type:

BitVector

classmethod frombin(string: str) Self[source]

Create a BitVector from a binary string. The string may contain any of ‘_’, ‘-’, ‘ ‘, or ‘:’. The string may start with “0b”.

Parameters:

string (str) – The binary string to convert

Returns:

The BitVector created from the binary string

Return type:

BitVector

classmethod fromhex(string: str) Self[source]

Create a BitVector from a hexadecimal string. The string may contain any of ‘_’, ‘-’, ‘ ‘, or ‘:’. The string may start with “0x”.

Parameters:

string (str) – The hexadecimal string to convert

Returns:

The BitVector created from the hexadecimal string

Return type:

BitVector

classmethod fromoct(string: str) Self[source]

Create a BitVector from an octal string. The string may contain any of ‘_’, ‘-’, ‘ ‘, or ‘:’. The string may start with “0o”.

Parameters:

string (str) – The octal string to convert

Returns:

The BitVector created from the octal string

Return type:

BitVector

classmethod fromsize(size: int, /) Self[source]

Create a BitVector with size bits, all set to 0.

Parameters:

size (int) – The size of the BitVector to create

Returns:

The BitVector created with the given size

Return type:

BitVector

hex(sep: str | None = None, bytes_per_sep: int = 1) str[source]

Convert the BitVector to a hexadecimal string prefixed by 0x. If sep is not None, the string is split into chunks of bytes_per_sep bytes

punctuated by sep.

Parameters:
  • sep (Optional[str]) – The separator to use

  • bytes_per_sep (int) – The number of bytes per separator

Returns:

The BitVector converted to a hexadecimal string

Return type:

str

index(value: BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray | int, start: int = 0, stop: int | None = None) int[source]

Finds the first occurrence of the given bit in the BitVector, or of the subsequence of bits if provided. If the bit is not found, a ValueError is raised.

Parameters:
  • value (Union[BitsConstructible, int]) – The bit to find

  • start (int, optional) – The first (leftmost) index to check. Defaults to 0.

  • stop (Optional[int], optional) – The index to give up on. Defaults to None.

Raises:

ValueError – If the bit is not found

Returns:

The index of the first occurrence of the bit

Return type:

int

insert(index: int, value: int) None[source]

Inserts the provided bit at the given index (zero-indexed). All bits at or to the right of the index are shifted right.

Parameters:
  • index (int) – The index at which to insert the bit

  • value (int) – The bit to insert

join(iterable: Iterable[BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray]) Self[source]
Concatenates the BitVectors in the iterable

with self as the separator.

Parameters:
  • self (Self) – The element-separating BitVector in the concatenation.

  • iterable (Iterable[BitsConstructible]) – The BitVectors to concatenate.

Returns:

A concatenation of the BitVectors in the iterable with

self between them.

Return type:

Self

lpad(width: int, fillbit: Literal[0, 1] = 0) Self[source]
lstrip(bits: Literal[0] | Literal[1] | None = None) Self[source]
Returns a new BitVector with contiguous leading instances of the

provided bit from the BitVector removed.

Defaults to removing leading 0s.

Parameters:
  • self (Self) – The bit to remove contiguous leading instances of.

  • bits (Optional[Union[Literal[0], Literal[1]]], optional) – The bit to remove. Defaults to None, removing leading 0s.

Returns:

The BitVector with the leading bits removed.

Return type:

Self

oct(sep: str | None = None, bytes_per_sep: int = 1) str[source]

Convert the BitVector to an octal string prefixed by 0x. If sep is not None, the string is split into chunks of bytes_per_sep bytes

punctuated by sep.

Parameters:
  • sep (Optional[str]) – The separator to use

  • bytes_per_sep (int) – The number of bytes per separator

Returns:

The BitVector converted to an octal string

Return type:

str

partition(sep: BitVector) tuple[Self, Self, Self][source]
Partitions the BitVector into three parts:

the part before the first occurrence of the separator, the separator itself, and the part after the separator.

If the separator is not found, the first part is the entire BitVector,

and the other two parts are empty BitVectors.

Parameters:

sep (BitVector) – The separator BitVector

Returns:

The three parts of the partition

Return type:

tuple[Self, Self, Self]

pop(index: int | None = None, default: T | None = None) int | T[source]

Removes and returns the bit at the given index (zero-indexed). All bits to the right of the index are shifted one left. If the provided index is None, the rightmost bit is popped. If a default is provided and the index is out of bounds, the default is returned.

Parameters:
  • index (Optional[int], optional) – The position of the bit to pop. Defaults to None.

  • default (Optional[T], optional) – The default value to return if the index is out of bounds. Defaults to None.

Raises:

IndexError – If the index is out of bounds and no default is provided.

Returns:

The bit at the given index or the default value

Return type:

Union[int, T]

remove(value: int) None[source]

Removes the first occurrence of the provided bit from the BitVector.

Parameters:

value (int) – The bit to remove

Raises:

ValueError – If the bit is not found in the BitVector

replace(old: BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray, new: BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray, count: int | None = None) Self[source]
Generates a new BitVector with occurrences of the sequences of

old bits replaced by the new bits.

If count is provided, only the first count occurrences are replaced.

Parameters:
  • old (BitsConstructible) – The bits to replace

  • new (BitsConstructible) – The bits to replace with

  • count (Optional[int], optional) – The maximum number of replacements. Defaults to None (maximal replacement).

Returns:

The new BitVector with the replacements made

Return type:

Self

reverse() None[source]

Reverses the bits in the BitVector.

rfind(value: BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray | int, start: int = 0, stop: int | None = None) int[source]

Finds the last occurrence of the given bit in the BitVector, or of the subsequence of bits if provided. If the bit is not found, -1 is returned.

Parameters:
  • value (Union[BitsConstructible, int]) – The bit to find

  • start (int, optional) – The last (leftmost) index to search. Defaults to 0.

  • stop (Optional[int], optional) – One over the index to start. Defaults to None.

Returns:

The index of the last occurrence of the bit, or -1 if not found

Return type:

int

rindex(value: BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray | int, start: int = 0, stop: int | None = None) int[source]

Finds the last occurrence of the given bit in the BitVector, or of the subsequence of bits if provided. If the bit is not found, a ValueError is raised.

Parameters:
  • value (Union[BitsConstructible, int]) – The bit to find

  • start (int, optional) – The last (leftmost) index to check. Defaults to 0.

  • stop (Optional[int], optional) – One over the index to start on. Defaults to None.

Raises:

ValueError – If the bit is not found

Returns:

The index of the last occurrence of the bit

Return type:

int

rpad(width: int, fillbit: Literal[0, 1] = 0) Self[source]
rpartition(sep: BitVector) tuple[Self, Self, Self][source]
Partitions the BitVector into three parts:

the part before the last occurrence of the separator, the separator itself, and the part after the separator.

If the separator is not found, the last part is the entire BitVector,

and the other two parts are empty BitVectors.

Parameters:

sep (BitVector) – The separator BitVector

Returns:

The three parts of the partition

Return type:

tuple[Self, Self, Self]

rstrip(bits: Literal[0] | Literal[1] | None = None) Self[source]
Returns a new BitVector with contiguous trailing instances of the

provided bit from the BitVector removed.

Defaults to removing trailing 0s.

Parameters:
  • self (Self) – The bit to remove contiguous trailing instances of.

  • bits (Optional[Union[Literal[0], Literal[1]]], optional) – The bit to remove. Defaults to None, removing trailing 0s.

Returns:

The BitVector with the trailing bits removed.

Return type:

Self

startswith(substrings: BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray | Literal[0, 1] | Iterable[BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray], start: int = 0, stop: int | None = None) bool[source]

Checks if the bitarray starts with the given substring. If start and stop are provided, the check is performed only

on the bits between the start (inclusive) and stop exclusive) indices.

Parameters:
  • substrings (Union[BitsConstructible, BitVector, Literal[0, 1], Iterable[Union[BitsConstructible, BitVector]]]) – The substring(s) to check

  • start (int, optional) – The start index. Defaults to 0.

  • stop (Optional[int], optional) – The stop index. Defaults to None.

strip(bits: Literal[0] | Literal[1] | None = None) Self[source]
Returns a new BitVector with contiguous leading and trailing instances of the

provided bit from the BitVector removed.

Defaults to removing leading and trailing 0s.

Parameters:
  • self (Self) – The bit to remove contiguous leading and trailing instances of.

  • bits (Optional[Union[Literal[0], Literal[1]]], optional) – The bit to remove. Defaults to None, removing leading and trailing 0s.

Returns:

A BitVector with the leading and trailing bits removed.

Return type:

Self

to01(sep: str | None = None, bytes_per_sep: int = 1) str[source]

Convert the BitVector to an unprefixed binary string. If sep is not None, the string is split into chunks of bytes_per_sep bytes

punctuated by sep.

to_bytes(reverse_endianness=False) bytes[source]
to_chararray(encoding: str | dict[Union[bytemaker.bitvector.bitvector_with_bitarray_speedup.BitVector, bytes, str, collections.abc.Iterable[Literal[0, 1]], bytemaker.bitvector.bitvector_with_bitarray_speedup.BitsCastable, bitarray.bitarray], str] = 'utf-8') str[source]

Convert the BitVector to a string where each byte is a character. The string is decoded using the given encoding. If this is a standard

byte encoding, bytes.decode is used to convert the bytes to a string.

Otherwise, the bytes are converted to strings using the given mapping.

Parameters:

encoding (Union[str, dict[BitsConstructible, str]]) – The encoding to use

Returns:

The BitVector converted to a string

Return type:

str

to_int(endianness: Literal['big', 'little'] = 'big', signed=True) int[source]
Converts a Bits object to an integer. It does this

by casting the Bits to bytes, and then converting the bytes to an integer using the provided endianness and signedness.

tobase(base: int, sep: str | None = None, bytes_per_sep: int = 1) str[source]

Convert the BitVector to a string in a given base. If sep is not None, the string is split into chunks of bytes_per_sep bytes

punctuated by sep.

Parameters:
  • base (int) – The base to convert to. Currently must be a power of 2 (< 64).

  • sep (Optional[str]) – The separator to use

  • bytes_per_sep (int) – The number of bytes per separator

Returns:

The BitVector converted to a string in the given base

Return type:

str

class bytemaker.bitvector.bitvector.BitsCastable(*args, **kwargs)[source]

Bases: Protocol

Protocol for objects that can be cast to a BitVector.

If provided object is not itself a BitVector, this protocol will be

prioritized when BitVectorSubtype(object) is called over any other possible behavior.

__Bits__ returns a BitVector representation of the object

that should not be a shallow copy (unless you want) the cast object to share memory with the original object).

class bytemaker.bitvector.bitvector_with_bitarray_speedup.BitVector(source: BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray | int | None = None, buffer: Buffer | None = None, *args, **kwargs)[source]

Bases: bitarray, MutableSequence[Literal[0, 1]]

A mutable sequence of bits.

This class’s behavior is largely a superset of that of bytearray.

excepting certain methods that are not applicable to bits.

This particular implementation is currently a

subclass of bitarray, which provides C-level performance for bit manipulation.

However, only the behavior documented here (that is, that does not stem

from the bitarray superclass) is guaranteed

append(value: int) None[source]

Appends the provided bit to end (right) of the BitVector.

Parameters:

value (int) – The bit to append

bin(sep: str | None = None, bytes_per_sep: int = 1) str[source]

Convert the BitVector to a binary string prefixed by 0x. If sep is not None, the string is split into chunks of bytes_per_sep bytes

punctuated by sep.

Parameters:
  • sep (Optional[str]) – The separator to use

  • bytes_per_sep (int) – The number of bytes per separator

Returns:

The BitVector converted to a binary string

Return type:

str

classmethod cast_if_not_bitarray(obj: BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray) Self | BitVector[source]

Casts the object to a BitVector if it is not already a BitVector.

Parameters:
  • cls (type[Self]) – The class of the object

  • obj (BitsConstructible) – The object to cast

Returns:

The object as a BitVector

Return type:

Union[Self, BitVector]

clear() None[source]

Removes all bits from the BitVector.

copy() Self[source]

Returns a shallow copy of the BitVector

count(value: BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray | int, start: int = 0, end: int | None = None) int[source]
Counts the occurrences of the given bit in the BitVector

between the given start (inclusive) and end (exclusive) indices.

Parameters:
  • value (Union[BitsConstructible, int]) – The bit to count

  • start (int, optional) – The start index. Defaults to 0.

  • end (Optional[int], optional) – The end index. Defaults to None.

Returns:

The number of occurrences of the bit

(within the provided index range, if any)

Return type:

int

endswith(substrings: BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray | Literal[0, 1] | Iterable[BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray], start: int = 0, stop: int | None = None) bool[source]

Checks if the bitarray ends with the given substring. If start and stop are provided, the check is performed only

on the bits between the start (inclusive) and stop exclusive) indices.

Parameters:
  • substrings (Union[BitsConstructible, BitVector, Literal[0, 1], Iterable[Union[BitsConstructible, BitVector]]]) – The substring(s) to check

  • start (int, optional) – The start index. Defaults to 0.

  • stop (Optional[int], optional) – The stop index. Defaults to None.

extend(values: BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray) None[source]
Extends the BitVector with the provided bits (appends them

on the right).

Parameters:

values (BitsConstructible) – The bits to append

find(value: BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray | int, start: int = 0, stop: int | None = None) int[source]

Finds the first occurrence of the given bit in the BitVector. If the bit is not found, -1 is returned.

Parameters:
  • value (Union[BitsConstructible, int]) – The bit to find

  • start (int, optional) – The initial index to search. Defaults to 0.

  • stop (Optional[int], optional) – The index to give up on. Defaults to None.

Returns:

The index of the first occurrence of the bit, or -1 if not found

Return type:

int

classmethod from01(string: str) Self[source]

Create a BitVector from a binary string. The string may contain any of ‘_’, ‘-’, ‘ ‘, or ‘:’. The string must contain only 0s and 1s other than these.

Parameters:

string (str) – The binary string to convert

Returns:

The BitVector created from the binary string

Return type:

BitVector

classmethod from_bytes(byte_arr: bytes, reverse_endianness=False)[source]
classmethod from_chararray(char_array: str, encoding: str | dict[str, Union[bytemaker.bitvector.bitvector_with_bitarray_speedup.BitVector, bytes, str, collections.abc.Iterable[Literal[0, 1]], bytemaker.bitvector.bitvector_with_bitarray_speedup.BitsCastable, bitarray.bitarray]] = 'utf-8') Self[source]

Create a BitVector from a char_array string where each character is a byte. The string is encoded using the given encoding. If this is a standard

byte encoding, str.encode is used to convert the string to bytes.

Otherwise, the string is converted to bytes using the given mapping,

iterating over the char_array and building up substrings until a substring is found in the encoding mapping to convert to a BitVector. These converted BitVectors are concatenated together to form the final returned BitVector.

Parameters:
  • char_array (str) – The string to convert

  • encoding (Union[str, dict[str, BitsConstructible]]) – The encoding to use

Returns:

The BitVector created from the string

Return type:

BitVector

classmethod from_int(integer: int, size=None)[source]
Converts an integer to a Bits object.

The size parameter determines the number of bits to use. If size is not provided, the number of bits required to represent the integer is used.

classmethod frombase(string: str, base: int) Self[source]

Create a BitVector from a string in a given base. The string may contain any of ‘_’, ‘-’, ‘ ‘, or ‘:’. In the case of bases 2, 8, and 16,

the string may start with “0b”, “0o”, or “0x” respectively.

Parameters:
  • string (str) – The string to convert

  • base (int) – The base of the string

Returns:

The BitVector created from the string

Return type:

BitVector

classmethod frombin(string: str) Self[source]

Create a BitVector from a binary string. The string may contain any of ‘_’, ‘-’, ‘ ‘, or ‘:’. The string may start with “0b”.

Parameters:

string (str) – The binary string to convert

Returns:

The BitVector created from the binary string

Return type:

BitVector

classmethod fromhex(string: str) Self[source]

Create a BitVector from a hexadecimal string. The string may contain any of ‘_’, ‘-’, ‘ ‘, or ‘:’. The string may start with “0x”.

Parameters:

string (str) – The hexadecimal string to convert

Returns:

The BitVector created from the hexadecimal string

Return type:

BitVector

classmethod fromoct(string: str) Self[source]

Create a BitVector from an octal string. The string may contain any of ‘_’, ‘-’, ‘ ‘, or ‘:’. The string may start with “0o”.

Parameters:

string (str) – The octal string to convert

Returns:

The BitVector created from the octal string

Return type:

BitVector

classmethod fromsize(size: int, /) Self[source]

Create a BitVector with size bits, all set to 0.

Parameters:

size (int) – The size of the BitVector to create

Returns:

The BitVector created with the given size

Return type:

BitVector

hex(sep: str | None = None, bytes_per_sep: int = 1) str[source]

Convert the BitVector to a hexadecimal string prefixed by 0x. If sep is not None, the string is split into chunks of bytes_per_sep bytes

punctuated by sep.

Parameters:
  • sep (Optional[str]) – The separator to use

  • bytes_per_sep (int) – The number of bytes per separator

Returns:

The BitVector converted to a hexadecimal string

Return type:

str

index(value: BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray | int, start: int = 0, stop: int | None = None) int[source]

Finds the first occurrence of the given bit in the BitVector, or of the subsequence of bits if provided. If the bit is not found, a ValueError is raised.

Parameters:
  • value (Union[BitsConstructible, int]) – The bit to find

  • start (int, optional) – The first (leftmost) index to check. Defaults to 0.

  • stop (Optional[int], optional) – The index to give up on. Defaults to None.

Raises:

ValueError – If the bit is not found

Returns:

The index of the first occurrence of the bit

Return type:

int

insert(index: int, value: int) None[source]

Inserts the provided bit at the given index (zero-indexed). All bits at or to the right of the index are shifted right.

Parameters:
  • index (int) – The index at which to insert the bit

  • value (int) – The bit to insert

join(iterable: Iterable[BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray]) Self[source]
Concatenates the BitVectors in the iterable

with self as the separator.

Parameters:
  • self (Self) – The element-separating BitVector in the concatenation.

  • iterable (Iterable[BitsConstructible]) – The BitVectors to concatenate.

Returns:

A concatenation of the BitVectors in the iterable with

self between them.

Return type:

Self

lpad(width: int, fillbit: Literal[0, 1] = 0) Self[source]
lstrip(bits: Literal[0] | Literal[1] | None = None) Self[source]
Returns a new BitVector with contiguous leading instances of the

provided bit from the BitVector removed.

Defaults to removing leading 0s.

Parameters:
  • self (Self) – The bit to remove contiguous leading instances of.

  • bits (Optional[Union[Literal[0], Literal[1]]], optional) – The bit to remove. Defaults to None, removing leading 0s.

Returns:

The BitVector with the leading bits removed.

Return type:

Self

oct(sep: str | None = None, bytes_per_sep: int = 1) str[source]

Convert the BitVector to an octal string prefixed by 0x. If sep is not None, the string is split into chunks of bytes_per_sep bytes

punctuated by sep.

Parameters:
  • sep (Optional[str]) – The separator to use

  • bytes_per_sep (int) – The number of bytes per separator

Returns:

The BitVector converted to an octal string

Return type:

str

partition(sep: BitVector) tuple[Self, Self, Self][source]
Partitions the BitVector into three parts:

the part before the first occurrence of the separator, the separator itself, and the part after the separator.

If the separator is not found, the first part is the entire BitVector,

and the other two parts are empty BitVectors.

Parameters:

sep (BitVector) – The separator BitVector

Returns:

The three parts of the partition

Return type:

tuple[Self, Self, Self]

pop(index: int | None = None, default: T | None = None) int | T[source]

Removes and returns the bit at the given index (zero-indexed). All bits to the right of the index are shifted one left. If the provided index is None, the rightmost bit is popped. If a default is provided and the index is out of bounds, the default is returned.

Parameters:
  • index (Optional[int], optional) – The position of the bit to pop. Defaults to None.

  • default (Optional[T], optional) – The default value to return if the index is out of bounds. Defaults to None.

Raises:

IndexError – If the index is out of bounds and no default is provided.

Returns:

The bit at the given index or the default value

Return type:

Union[int, T]

remove(value: int) None[source]

Removes the first occurrence of the provided bit from the BitVector.

Parameters:

value (int) – The bit to remove

Raises:

ValueError – If the bit is not found in the BitVector

replace(old: BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray, new: BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray, count: int | None = None) Self[source]
Generates a new BitVector with occurrences of the sequences of

old bits replaced by the new bits.

If count is provided, only the first count occurrences are replaced.

Parameters:
  • old (BitsConstructible) – The bits to replace

  • new (BitsConstructible) – The bits to replace with

  • count (Optional[int], optional) – The maximum number of replacements. Defaults to None (maximal replacement).

Returns:

The new BitVector with the replacements made

Return type:

Self

reverse() None[source]

Reverses the bits in the BitVector.

rfind(value: BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray | int, start: int = 0, stop: int | None = None) int[source]

Finds the last occurrence of the given bit in the BitVector, or of the subsequence of bits if provided. If the bit is not found, -1 is returned.

Parameters:
  • value (Union[BitsConstructible, int]) – The bit to find

  • start (int, optional) – The last (leftmost) index to search. Defaults to 0.

  • stop (Optional[int], optional) – One over the index to start. Defaults to None.

Returns:

The index of the last occurrence of the bit, or -1 if not found

Return type:

int

rindex(value: BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray | int, start: int = 0, stop: int | None = None) int[source]

Finds the last occurrence of the given bit in the BitVector, or of the subsequence of bits if provided. If the bit is not found, a ValueError is raised.

Parameters:
  • value (Union[BitsConstructible, int]) – The bit to find

  • start (int, optional) – The last (leftmost) index to check. Defaults to 0.

  • stop (Optional[int], optional) – One over the index to start on. Defaults to None.

Raises:

ValueError – If the bit is not found

Returns:

The index of the last occurrence of the bit

Return type:

int

rpad(width: int, fillbit: Literal[0, 1] = 0) Self[source]
rpartition(sep: BitVector) tuple[Self, Self, Self][source]
Partitions the BitVector into three parts:

the part before the last occurrence of the separator, the separator itself, and the part after the separator.

If the separator is not found, the last part is the entire BitVector,

and the other two parts are empty BitVectors.

Parameters:

sep (BitVector) – The separator BitVector

Returns:

The three parts of the partition

Return type:

tuple[Self, Self, Self]

rstrip(bits: Literal[0] | Literal[1] | None = None) Self[source]
Returns a new BitVector with contiguous trailing instances of the

provided bit from the BitVector removed.

Defaults to removing trailing 0s.

Parameters:
  • self (Self) – The bit to remove contiguous trailing instances of.

  • bits (Optional[Union[Literal[0], Literal[1]]], optional) – The bit to remove. Defaults to None, removing trailing 0s.

Returns:

The BitVector with the trailing bits removed.

Return type:

Self

startswith(substrings: BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray | Literal[0, 1] | Iterable[BitVector | bytes | str | Iterable[Literal[0, 1]] | BitsCastable | bitarray], start: int = 0, stop: int | None = None) bool[source]

Checks if the bitarray starts with the given substring. If start and stop are provided, the check is performed only

on the bits between the start (inclusive) and stop exclusive) indices.

Parameters:
  • substrings (Union[BitsConstructible, BitVector, Literal[0, 1], Iterable[Union[BitsConstructible, BitVector]]]) – The substring(s) to check

  • start (int, optional) – The start index. Defaults to 0.

  • stop (Optional[int], optional) – The stop index. Defaults to None.

strip(bits: Literal[0] | Literal[1] | None = None) Self[source]
Returns a new BitVector with contiguous leading and trailing instances of the

provided bit from the BitVector removed.

Defaults to removing leading and trailing 0s.

Parameters:
  • self (Self) – The bit to remove contiguous leading and trailing instances of.

  • bits (Optional[Union[Literal[0], Literal[1]]], optional) – The bit to remove. Defaults to None, removing leading and trailing 0s.

Returns:

A BitVector with the leading and trailing bits removed.

Return type:

Self

to01(sep: str | None = None, bytes_per_sep: int = 1) str[source]

Convert the BitVector to an unprefixed binary string. If sep is not None, the string is split into chunks of bytes_per_sep bytes

punctuated by sep.

to_bytes(reverse_endianness=False) bytes[source]
to_chararray(encoding: str | dict[Union[bytemaker.bitvector.bitvector_with_bitarray_speedup.BitVector, bytes, str, collections.abc.Iterable[Literal[0, 1]], bytemaker.bitvector.bitvector_with_bitarray_speedup.BitsCastable, bitarray.bitarray], str] = 'utf-8') str[source]

Convert the BitVector to a string where each byte is a character. The string is decoded using the given encoding. If this is a standard

byte encoding, bytes.decode is used to convert the bytes to a string.

Otherwise, the bytes are converted to strings using the given mapping.

Parameters:

encoding (Union[str, dict[BitsConstructible, str]]) – The encoding to use

Returns:

The BitVector converted to a string

Return type:

str

to_int(endianness: Literal['big', 'little'] = 'big', signed=True) int[source]
Converts a Bits object to an integer. It does this

by casting the Bits to bytes, and then converting the bytes to an integer using the provided endianness and signedness.

tobase(base: int, sep: str | None = None, bytes_per_sep: int = 1) str[source]

Convert the BitVector to a string in a given base. If sep is not None, the string is split into chunks of bytes_per_sep bytes

punctuated by sep.

Parameters:
  • base (int) – The base to convert to. Currently must be a power of 2 (< 64).

  • sep (Optional[str]) – The separator to use

  • bytes_per_sep (int) – The number of bytes per separator

Returns:

The BitVector converted to a string in the given base

Return type:

str

class bytemaker.bitvector.bitvector_with_bitarray_speedup.BitsCastable(*args, **kwargs)[source]

Bases: Protocol

Protocol for objects that can be cast to a BitVector.

If provided object is not itself a BitVector, this protocol will be

prioritized when BitVectorSubtype(object) is called over any other possible behavior.

__Bits__ returns a BitVector representation of the object

that should not be a shallow copy (unless you want) the cast object to share memory with the original object).

bytemaker.bitvector.bitvector_with_bitarray_speedup.BitsConstructible

The types that can be used to construct a BitVector. These include the BitVector class itself, bytes, str, iterables of 0s and 1s, objects that can be cast to a BitVector, and bitarrays.

Please note that you can also use an int to construct a BitVector of that many zeroes, but this is not included in the type hint because it is less implicitly a series of bits.

alias of Union[BitVector, bytes, str, Iterable[Literal[0, 1]], BitsCastable, bitarray]