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:
- classmethod from_signed_int(value, length)[source]
Converts a signed integer to a bit array using off-set binary representation.
- Parameters:
- Returns:
A BitArray representing the bits of the signed integer.
- Return type:
- Raises:
AssertionError – If the value is out of range for the specified length.
- 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:
- 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:
- 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:
- 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:
- 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).
- copy()[source]
Creates a copy of the bit array.
- Returns:
A new BitArray with the same bits.
- Return type:
- __len__()[source]
Returns the length of the bit array.
- Returns:
The number of bits in the array.
- Return type:
- __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.
- __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:
- __bool__()[source]
Returns True if any bit is set.
- Returns:
True if any bit is set, False otherwise.
- Return type:
- __repr__()[source]
Returns a string representation of the BitArray.
- Returns:
String representation of the BitArray.
- Return type:
- __str__()[source]
Returns a string representation of the bits.
- Returns:
String representation of the bits.
- Return type:
- __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:
- all()[source]
Returns True if all bits are set to True.
- Returns:
True if all bits are set to True, False otherwise.
- Return type: