Minor adjustments to bitutilities.py.

This commit is contained in:
Rhinemann 2023-06-26 21:58:24 +03:00
parent a9cfd1ec97
commit 8f68099309
1 changed files with 23 additions and 11 deletions

View File

@ -1,8 +1,10 @@
from collections import deque
from typing_extensions import Self
class BasicRegister:
"""The BasicRegister represents a hardware register capable of manipulating multiple bits at a time.
"""
The BasicRegister represents a hardware register capable of manipulating multiple bits at a time.
:param deque[bool] memory: The bits stored inside the register.
"""
@ -14,6 +16,20 @@ class BasicRegister:
def __str__(self) -> str:
return f"Memory: {[int(value) for value in self.memory]}"
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])
@ -27,10 +43,6 @@ class BasicRegister:
shifted_radices: deque[bool] = deque([self.memory.pop() for _i in range(steps_shifted)])
return shifted_radices
def adjust_size(self, s: int) -> None:
current_memory_size: int = len(self.memory)
return BasicRegister([False] * max(s - current_memory_size, 0) + list(self.memory)[-current_memory_size:])
def get_memory(variable_name: str) -> list[bool]:
"""
@ -50,14 +62,14 @@ def get_memory(variable_name: str) -> list[bool]:
print(f"[ERROR] The {variable_name} may contain only 1-s and 0-s!")
def sum(a_original: BasicRegister, b_original: BasicRegister) -> BasicRegister:
def binary_sum(a_original: BasicRegister, b_original: BasicRegister) -> BasicRegister:
"""
Sums two registers' values.
:param BasicRegister a: First register.
:param BasicRegister b: Second register.
:param BasicRegister a_original: First register.
:param BasicRegister b_original: Second register.
:return: Register containing the result.
:return: Register containing the sum.
:rtype: BasicRegister
"""
size_a = len(a_original.memory)
@ -66,8 +78,8 @@ def sum(a_original: BasicRegister, b_original: BasicRegister) -> BasicRegister:
required_size = max(size_a, size_b)
if size_a != size_b:
a = a_original.adjust_size(required_size)
b = b_original.adjust_size(required_size)
a = a_original.adjusted_by_size(required_size)
b = b_original.adjusted_by_size(required_size)
else:
a = a_original
b = b_original