The bitdef and associated functions.
A bitdef is a string made up of
.means that the bit at this position could be a 0 or a 1.
0means that the bit at this position is a 0.
1means that the bit at this position is a 1.
When indexing into a bitdef, indexes start at 0 and begin at the right hand side or least significant bit of the value. E.g.:
Index: 76543210 Bitdef: 010.1..1
Check if the passed in bitdefs are all the same length.
Parameters: list (bitdefs) – Bitdefs to check length of. Returns: True if all the bitdefs are the same length, False otherwise Return type: bool
Calculate length of a bitdef.
Parameters: bitdef (str) – The bitdef to find the length of. Returns: The length of the bitdef. Return type: int
Check if the bitdefs have any bits set in the same position.
Example with overlap (bits at index 2 and 6 overlap):
Example with no overlap:
Parameters: bitdefs (list(str)) – Bitdefs to check for overlaps. Returns: Whether or not there were overlaps. Return type: bool
Merge the bitdefs to a single bitdef.
- All be the same length.
- Not have any bits defined in the same position.
bitdefs (list(str)) – Bitdefs to merge.
The merged bitdef.
ValueError– If the bitdefs are not all the same length or have
- overlapping bits.
Collapse undefined bits into real bits to make new bitdefs.
The undefined bits are expanded in order, from left to right, with 0 first, then 1.
Parameters: bitdef (str) – The bitdef to collapse. Returns: The list of bitdefs the original bitdef has collapsed to. Return type: list(str)
Fill undefined bits with a value.
111010011when filled with 1s.
- bitdef (str) – The bitdef to fill.
- value (str) – The value to fill with, “0” or “1”.
The filled bitdef.
extract_bits(bitdef, end, start)¶
Extract a region from the bitdef.
Indexes for start and end start at zero from the right or least significant bit.
For example, if the bitdef was
00101011and the extraction end was 4 and start was 1 the result would be
Extracted bits: xxxx Index: 76543210 Bitdef: 00101011 Result: 0101
- bitdef (str) – The bitdef to extract bits from.
- end (int) – Index of the leftmost bit of the portion to extract.
- start (int) – Index of the rightmost bit of the portion to extract.
The extracted portion of the bitdef.
- Extraction region is larger than bitdef.
- Extraction end index is before extraction start index.
- Extraction start index is less than 0.
Remove the whitespace from a string.
Parameters: input_string (str) – The string to remove whitespace from. Returns: The string with the whitespace removed. Return type: str
Reverse the passed in index as if the index direction was flipped.
Taking the string “hello” as an example the regular indexes for each letter are:
Reversing the indexes yields:
This allows easily indexing into a bitdef on bitdef indexing terms.
- index (int) – The index position to reverse.
- length (int) – The length of the array being indexed into.
The reversed index.