eight_bit_computer.number_utils module

Functions for working with, checking and converting numbers.

All numbers are stored within the computer as the positive equivalent. They may be interpreted as negative.

eight_bit_computer.number_utils.number_to_bitstring(number, bit_width=8)[source]

Convert a number to an equivalent bitstring of the given width.

Raises:ValueError – If number doesn’t fit in the bit width.
eight_bit_computer.number_utils.number_is_within_bit_limit(number, bit_width=8)[source]

Check if a number can be stored in the number of bits given.

Negative numbers are stored in 2’s compliment binary.

Parameters:
  • number (int) – The number to check.
  • bit_width (int, optional) – The number of bits available.
Returns:

True if within limits, False if not.

Return type:

bool

eight_bit_computer.number_utils.get_positive_equivalent(number)[source]

Read the 2’s compliment equivalent of this number as positive.

With a 3 bit number, the positive equivalent of -2 is 5. E.g.:

-4 4 100
-3 5 101
-2 6 110
-1 7 111
 0 0 000
 1 1 001
 2 2 010
 3 3 011
Parameters:number (int) – The number to convert to a positive quivalent
Returns:The positive equivalent of the number.
Return type:int
eight_bit_computer.number_utils.bitstring_to_number(bitstring)[source]

Convert a bitstring to a number.

E.g. 10110101 gives 181.

Parameters:bitstring (str) – String of 1s and 0s.
Returns:The equivalent integer.
Return type:int
eight_bit_computer.number_utils.bitstring_to_hex_string(bitstring, zero_pad_width=2)[source]

Convert a bitstring to a hex number.

Parameters:
  • bitstring (str) – String of 1s and 0s.
  • zero_pad_width (int) (optional) – How many zeroes to pad the returned hex value with.