Compare commits

..

No commits in common. "544031efbbcfbbf5ef871f1a4dd520db46f97024" and "f2ee4cd7092e9e9a4b3d9f3daed651db7a8055af" have entirely different histories.

2 changed files with 31 additions and 44 deletions

View File

@ -10,17 +10,12 @@ class BasicRegister:
"""
def __init__(self, memory: list[bool]):
"""Constructor method"""
self.memory: deque[bool] = deque(memory)
def __repr__(self) -> str:
return f"Memory: {[int(value) for value in self.memory]}"
def __str__(self) -> str:
return f"Memory: {[int(value) for value in self.memory]}"
def __len__(self) -> int:
return len(self.memory)
def adjusted_by_size(self, resulting_size: int) -> Self:
"""
Adjusts a register to a given size.
@ -67,29 +62,27 @@ def get_memory(variable_name: str) -> list[bool]:
print(f"[ERROR] The {variable_name} may contain only 1-s and 0-s!")
def binary_sum(first_term: BasicRegister, second_term: BasicRegister, return_remainder: bool = False)\
-> BasicRegister | tuple[BasicRegister, int]:
def binary_sum(a_original: BasicRegister, b_original: BasicRegister) -> BasicRegister:
"""
Sums two registers' values.
:param BasicRegister first_term: First register.
:param BasicRegister second_term: Second register.
:param bool return_remainder: True to return the tuple, False to return just the register.
:param BasicRegister a_original: First register.
:param BasicRegister b_original: Second register.
:return: Register containing the sum or the tuple containing the register and carried radix.
:rtype: BasicRegister | tuple[BasicRegister, int]
:return: Register containing the sum.
:rtype: BasicRegister
"""
size_a = len(first_term)
size_b = len(second_term)
size_a = len(a_original.memory)
size_b = len(b_original.memory)
required_size = max(size_a, size_b)
a = first_term
b = second_term
if size_a != size_b:
a = a.adjusted_by_size(required_size)
b = b.adjusted_by_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
c = BasicRegister([False] * required_size)
@ -102,7 +95,4 @@ def binary_sum(first_term: BasicRegister, second_term: BasicRegister, return_rem
final_bit_sum = a.memory[0] + b.memory[0] + carry
c.memory[0] = bool(final_bit_sum & 1)
if return_remainder:
return c, carry
else:
return c
return c

37
main.py
View File

@ -1,31 +1,28 @@
import bitutilities as bu
from bitutilities import *
import timeit
if __name__ == '__main__':
reg: bu.BasicRegister = bu.BasicRegister(bu.get_memory("memory"))
reg: BasicRegister = BasicRegister(get_memory("memory"))
# print(type(reg))
print()
print("Register 1:")
print("\nRegister:")
print(reg)
print()
reg2: bu.BasicRegister = bu.BasicRegister(bu.get_memory("more memory"))
print("\nReversed:")
reg.reverse()
print(reg)
print()
print("Register 2:")
print(reg2)
print("\nShifted left:")
print([int(value) for value in reg.left_shift()])
print(reg)
print()
reg3: bu.BasicRegister = bu.binary_sum(reg, reg2)
print("\nShifted right:")
print([int(value) for value in reg.right_shift()])
print(reg)
reg2: BasicRegister = BasicRegister(get_memory("more memory"))
reg3: BasicRegister = binary_sum(reg, reg2)
print()
print("Sum:")
print(reg3)
carry_sum_test: tuple[bu.BasicRegister, int] = bu.binary_sum(reg, reg2, True)
print()
print("Sum & carry:")
# print(type(carry_sum_test))
print(carry_sum_test)