eight_bit_computer.bitdef module¶
The bitdef and associated functions.
A bitdef is a string made up of .
s, 0
s, and 1
s.
.
means that the bit at this position could be a 0 or a 1.0
means that the bit at this position is a 0.1
means 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
-
eight_bit_computer.bitdef.
same_length
(bitdefs)[source]¶ 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
-
eight_bit_computer.bitdef.
length
(bitdef)[source]¶ Calculate length of a bitdef.
Parameters: bitdef (str) – The bitdef to find the length of. Returns: The length of the bitdef. Return type: int
-
eight_bit_computer.bitdef.
have_overlapping_bits
(bitdefs)[source]¶ Check if the bitdefs have any bits set in the same position.
Example with overlap (bits at index 2 and 6 overlap):
0...101.
11...1..
Example with no overlap:
11010...
......11
Parameters: bitdefs (list(str)) – Bitdefs to check for overlaps. Returns: Whether or not there were overlaps. Return type: bool
-
eight_bit_computer.bitdef.
merge
(bitdefs)[source]¶ Merge the bitdefs to a single bitdef.
Bitdefs must
- All be the same length.
- Not have any bits defined in the same position.
Parameters: bitdefs (list(str)) – Bitdefs to merge.
Returns: The merged bitdef.
Return type: str
Raises: ValueError
– If the bitdefs are not all the same length or have- overlapping bits.
-
eight_bit_computer.bitdef.
collapse
(bitdef)[source]¶ 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.
For example,
10.0.
becomes:10000
10001
10100
10101
Parameters: bitdef (str) – The bitdef to collapse. Returns: The list of bitdefs the original bitdef has collapsed to. Return type: list(str)
-
eight_bit_computer.bitdef.
fill
(bitdef, value)[source]¶ Fill undefined bits with a value.
For example
1..0100.1
becomes111010011
when filled with 1s.Parameters: - bitdef (str) – The bitdef to fill.
- value (str) – The value to fill with, “0” or “1”.
Returns: The filled bitdef.
Return type: str
-
eight_bit_computer.bitdef.
extract_bits
(bitdef, end, start)[source]¶ 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
00101011
and the extraction end was 4 and start was 1 the result would be0101
:Extracted bits: xxxx Index: 76543210 Bitdef: 00101011 Result: 0101
Parameters: - 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.
Returns: The extracted portion of the bitdef.
Return type: str
Raises: ValueError
– If:- Extraction region is larger than bitdef.
- Extraction end index is before extraction start index.
- Extraction start index is less than 0.
-
eight_bit_computer.bitdef.
remove_whitespace
(input_string)[source]¶ 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
-
eight_bit_computer.bitdef.
reverse_index
(index, length)[source]¶ 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:
01234 hello
Reversing the indexes yields:
43210 hello
This allows easily indexing into a bitdef on bitdef indexing terms.
Parameters: - index (int) – The index position to reverse.
- length (int) – The length of the array being indexed into.
Returns: The reversed index.
Return type: int