eight_bit_computer.bitdef module

The bitdef and associated functions.

A bitdef is a string made up of .s, 0s, and 1s.

  • . 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

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):

  • 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

Merge the bitdefs to a single bitdef.

Bitdefs must

  • All be the same length.
  • Not have any bits defined in the same position.

bitdefs (list(str)) – Bitdefs to merge.


The merged bitdef.

Return type:


  • 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.

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 becomes 111010011 when filled with 1s.

  • bitdef (str) – The bitdef to fill.
  • value (str) – The value to fill with, “0” or “1”.

The filled bitdef.

Return type:


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 be 0101:

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.

Return type:



ValueError – If:

  • 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
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:


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.

Return type: