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