flexfloat.BitArray

class flexfloat.BitArray(*args, **kwargs)[source]

Protocol defining the interface for BitArray implementations.

This protocol defines all the methods and properties that a BitArray implementation must provide.

For consistency, all BitArray implementations should order bits as LSB-first, meaning the least significant bit is at index 0 and the most significant bit is at the highest index.

classmethod from_bits(bits=None)[source]

Creates a BitArray from a list of boolean values.

Parameters:

bits (list[bool] | None, optional) – List of boolean values. Defaults to None, which creates an empty BitArray.

Returns:

A BitArray created from the bits.

Return type:

BitArray

classmethod from_float(value)[source]

Converts a floating-point number to a bit array.

Parameters:

value (float) – The floating-point number to convert.

Returns:

A BitArray representing the bits of the floating-point number.

Return type:

BitArray

classmethod from_signed_int(value, length)[source]

Converts a signed integer to a bit array using off-set binary representation.

Parameters:
  • value (int) – The signed integer to convert.

  • length (int) – The length of the resulting bit array.

Returns:

A BitArray representing the bits of the signed integer.

Return type:

BitArray

Raises:

AssertionError – If the value is out of range for the specified length.

classmethod zeros(length)[source]

Creates a BitArray filled with zeros.

Parameters:

length (int) – The length of the bit array.

Returns:

A BitArray filled with False values.

Return type:

BitArray

classmethod ones(length)[source]

Creates a BitArray filled with ones.

Parameters:

length (int) – The length of the bit array.

Returns:

A BitArray filled with True values.

Return type:

BitArray

classmethod parse_bitarray(bitstring)[source]

Parses a string of bits (with optional spaces) into a BitArray instance. Non-valid characters are ignored.

Parameters:

bitstring (Iterable[str]) – A string of bits, e.g., “1010 1100”.

Returns:

A BitArray instance created from the bit string.

Return type:

BitArray

to_float()[source]

Converts a 64-bit array to a floating-point number.

Returns:

The floating-point number represented by the bit array.

Return type:

float

Raises:

AssertionError – If the bit array is not 64 bits long.

to_int()[source]

Converts the bit array to an unsigned integer.

Returns:

The integer represented by the bit array.

Return type:

int

to_signed_int()[source]

Converts a bit array into a signed integer using off-set binary representation.

Returns:

The signed integer represented by the bit array.

Return type:

int

Raises:

AssertionError – If the bit array is empty.

shift(shift_amount, fill=False)[source]

Shifts the bit array left or right by a specified number of bits.

This function shifts the bits in the array, filling in new bits with the specified fill value. If the value is positive, it shifts left; if negative, it shifts right. Fills the new bits with the specified fill value (default is False).

Parameters:
  • shift_amount (int) – The number of bits to shift. Positive for left shift, negative for right shift.

  • fill (bool, optional) – The value to fill in the new bits created by the shift. Defaults to False.

Returns:

A new BitArray with the bits shifted and filled.

Return type:

BitArray

copy()[source]

Creates a copy of the bit array.

Returns:

A new BitArray with the same bits.

Return type:

BitArray

__len__()[source]

Returns the length of the bit array.

Returns:

The number of bits in the array.

Return type:

int

__getitem__(index: int) bool[source]
__getitem__(index: slice) BitArray

Get a bit or a slice of bits as a new BitArray.

__setitem__(index: int, value: bool) None[source]
__setitem__(index: slice, value: 'BitArray' | list[bool]) None

Sets an item or slice in the bit array.

Parameters:
  • index (int or slice) – The index or slice to set.

  • value (bool or list[bool] or BitArray) – The value(s) to assign.

__iter__()[source]

Iterates over the bits in the array.

Yields:

bool – The next bit in the array.

__add__(other)[source]

Concatenates two bit arrays.

Parameters:

other (BitArray or list[bool]) – The other bit array or list to concatenate.

Returns:

The concatenated bit array.

Return type:

BitArray

__radd__(other)[source]

Reverse concatenation with a list.

Parameters:

other (list[bool]) – The list to concatenate before this bit array.

Returns:

The concatenated bit array.

Return type:

BitArray

__eq__(other)[source]

Checks equality with another BitArray or list.

Parameters:

other (object) – The object to compare with.

Returns:

True if equal, False otherwise.

Return type:

bool

__bool__()[source]

Returns True if any bit is set.

Returns:

True if any bit is set, False otherwise.

Return type:

bool

__repr__()[source]

Returns a string representation of the BitArray.

Returns:

String representation of the BitArray.

Return type:

str

__str__()[source]

Returns a string representation of the bits.

Returns:

String representation of the bits.

Return type:

str

__init__(*args, **kwargs)
any()[source]

Returns True if any bit is set to True.

Returns:

True if any bit is set to True, False otherwise.

Return type:

bool

all()[source]

Returns True if all bits are set to True.

Returns:

True if all bits are set to True, False otherwise.

Return type:

bool

count(value=True)[source]

Counts the number of bits set to the specified value.

Parameters:

value (bool, optional) – The value to count. Defaults to True.

Returns:

The number of bits set to the specified value.

Return type:

int

reverse()[source]

Returns a new BitArray with the bits in reverse order.

Returns:

A new BitArray with the bits in reverse order.

Return type:

BitArray