Compare commits
4 Commits
f2ee4cd709
...
544031efbb
Author | SHA1 | Date |
---|---|---|
Rhinemann | 544031efbb | |
Rhinemann | d2df4bbf28 | |
Rhinemann | 132a0b5659 | |
Rhinemann | 75f7dba546 |
|
@ -10,12 +10,17 @@ 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.
|
||||
|
@ -62,27 +67,29 @@ 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(a_original: BasicRegister, b_original: BasicRegister) -> BasicRegister:
|
||||
def binary_sum(first_term: BasicRegister, second_term: BasicRegister, return_remainder: bool = False)\
|
||||
-> BasicRegister | tuple[BasicRegister, int]:
|
||||
"""
|
||||
Sums two registers' values.
|
||||
|
||||
:param BasicRegister a_original: First register.
|
||||
:param BasicRegister b_original: Second register.
|
||||
: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.
|
||||
|
||||
:return: Register containing the sum.
|
||||
:rtype: BasicRegister
|
||||
:return: Register containing the sum or the tuple containing the register and carried radix.
|
||||
:rtype: BasicRegister | tuple[BasicRegister, int]
|
||||
"""
|
||||
size_a = len(a_original.memory)
|
||||
size_b = len(b_original.memory)
|
||||
size_a = len(first_term)
|
||||
size_b = len(second_term)
|
||||
|
||||
required_size = max(size_a, size_b)
|
||||
|
||||
a = first_term
|
||||
b = second_term
|
||||
|
||||
if size_a != size_b:
|
||||
a = a_original.adjusted_by_size(required_size)
|
||||
b = b_original.adjusted_by_size(required_size)
|
||||
else:
|
||||
a = a_original
|
||||
b = b_original
|
||||
a = a.adjusted_by_size(required_size)
|
||||
b = b.adjusted_by_size(required_size)
|
||||
|
||||
c = BasicRegister([False] * required_size)
|
||||
|
||||
|
@ -95,4 +102,7 @@ def binary_sum(a_original: BasicRegister, b_original: BasicRegister) -> BasicReg
|
|||
final_bit_sum = a.memory[0] + b.memory[0] + carry
|
||||
c.memory[0] = bool(final_bit_sum & 1)
|
||||
|
||||
return c
|
||||
if return_remainder:
|
||||
return c, carry
|
||||
else:
|
||||
return c
|
||||
|
|
37
main.py
37
main.py
|
@ -1,28 +1,31 @@
|
|||
from bitutilities import *
|
||||
import bitutilities as bu
|
||||
import timeit
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
reg: BasicRegister = BasicRegister(get_memory("memory"))
|
||||
# print(type(reg))
|
||||
reg: bu.BasicRegister = bu.BasicRegister(bu.get_memory("memory"))
|
||||
|
||||
print("\nRegister:")
|
||||
print()
|
||||
print("Register 1:")
|
||||
print(reg)
|
||||
|
||||
print("\nReversed:")
|
||||
reg.reverse()
|
||||
print(reg)
|
||||
print()
|
||||
reg2: bu.BasicRegister = bu.BasicRegister(bu.get_memory("more memory"))
|
||||
|
||||
print("\nShifted left:")
|
||||
print([int(value) for value in reg.left_shift()])
|
||||
print(reg)
|
||||
print()
|
||||
print("Register 2:")
|
||||
print(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()
|
||||
reg3: bu.BasicRegister = bu.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)
|
||||
|
|
Loading…
Reference in New Issue