binaryCalculatorPrototype/bitutilities.py

99 lines
3.2 KiB
Python
Raw Normal View History

from collections import deque
2023-06-26 21:58:24 +03:00
from typing_extensions import Self
class BasicRegister:
2023-06-26 21:58:24 +03:00
"""
The BasicRegister represents a hardware register capable of manipulating multiple bits at a time.
:param deque[bool] memory: The bits stored inside the register.
"""
def __init__(self, memory: list[bool]):
"""Constructor method"""
self.memory: deque[bool] = deque(memory)
def __str__(self) -> str:
return f"Memory: {[int(value) for value in self.memory]}"
2023-06-26 21:58:24 +03:00
def adjusted_by_size(self, resulting_size: int) -> Self:
"""
Adjusts a register to a given size.
:param int resulting_size: The size of the resulting register.
:return: A register of a specified size.
:rtype: BasicRegister
"""
current_memory_size: int = len(self.memory)
return BasicRegister(
[False] * max(resulting_size - current_memory_size, 0) + list(self.memory)[-current_memory_size:]
)
def reverse(self):
self.memory = deque([not value for value in self.memory])
def left_shift(self, digit_to_fill: bool = False, steps_shifted: int = 1) -> deque[bool]:
self.memory.extend([digit_to_fill] * steps_shifted)
shifted_radices: deque[bool] = deque([self.memory.popleft() for _i in range(steps_shifted)])
return shifted_radices
def right_shift(self, digit_to_fill: bool = False, steps_shifted: int = 1) -> deque[bool]:
self.memory.extendleft([digit_to_fill] * steps_shifted)
shifted_radices: deque[bool] = deque([self.memory.pop() for _i in range(steps_shifted)])
return shifted_radices
def get_memory(variable_name: str) -> list[bool]:
"""
Reads user input to be used as a memory array.
:param str variable_name: The name to be displayed in the input line.
:return: A list of boolean values read from user.
:rtype: list[bool]
"""
while True:
input_chars: list[str] = list(input(f"Enter {variable_name}: "))
if all(character in ["0", "1"] for character in input_chars):
return [True if character == "1" else False for character in input_chars]
else:
print(f"[ERROR] The {variable_name} may contain only 1-s and 0-s!")
2023-06-26 21:58:24 +03:00
def binary_sum(a_original: BasicRegister, b_original: BasicRegister) -> BasicRegister:
"""
Sums two registers' values.
2023-06-26 21:58:24 +03:00
:param BasicRegister a_original: First register.
:param BasicRegister b_original: Second register.
2023-06-26 21:58:24 +03:00
:return: Register containing the sum.
:rtype: BasicRegister
"""
size_a = len(a_original.memory)
size_b = len(b_original.memory)
required_size = max(size_a, size_b)
if size_a != size_b:
2023-06-26 21:58:24 +03:00
a = a_original.adjusted_by_size(required_size)
b = b_original.adjusted_by_size(required_size)
else:
a = a_original
b = b_original
c = BasicRegister([False] * required_size)
carry = False
for i in range(size_a - 1, 0, -1):
current_bit_sum = a.memory[i] + b.memory[i] + carry
carry = bool(current_bit_sum & 2)
c.memory[i] = bool(current_bit_sum & 1)
final_bit_sum = a.memory[0] + b.memory[0] + carry
c.memory[0] = bool(final_bit_sum & 1)
return c