Revamped function scopes.
This commit is contained in:
parent
f477c6f9e1
commit
b3c5011c11
|
@ -67,7 +67,7 @@ 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)\
|
||||
def binary_sum(first_term: BasicRegister, second_term: BasicRegister, return_remainder: bool = False) \
|
||||
-> BasicRegister or tuple[BasicRegister, int]:
|
||||
"""
|
||||
Sums two registers' values.
|
||||
|
@ -79,61 +79,50 @@ def binary_sum(first_term: BasicRegister, second_term: BasicRegister, return_rem
|
|||
:return: Register containing the sum or the tuple containing the register and carried radix.
|
||||
:rtype: BasicRegister | tuple[BasicRegister, int]
|
||||
"""
|
||||
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.adjusted_by_size(required_size)
|
||||
b = b.adjusted_by_size(required_size)
|
||||
|
||||
c = BasicRegister([False] * required_size)
|
||||
result = BasicRegister([False] * len(first_term))
|
||||
|
||||
carry = False
|
||||
for i in range(required_size - 1, 0, -1):
|
||||
current_bit_sum = a.memory[i] + b.memory[i] + carry
|
||||
for i in range(len(first_term) - 1, 0, -1):
|
||||
current_bit_sum = first_term.memory[i] + second_term.memory[i] + carry
|
||||
carry = bool(current_bit_sum & 2)
|
||||
c.memory[i] = bool(current_bit_sum & 1)
|
||||
result.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)
|
||||
final_bit_sum = first_term.memory[0] + second_term.memory[0] + carry
|
||||
result.memory[0] = bool(final_bit_sum & 1)
|
||||
|
||||
if return_remainder:
|
||||
final_carry = bool(final_bit_sum & 2)
|
||||
return c, final_carry
|
||||
return result, final_carry
|
||||
else:
|
||||
return c
|
||||
return result
|
||||
|
||||
|
||||
def binary_subtraction_1_complement(first_term: BasicRegister, second_term: BasicRegister) -> BasicRegister:
|
||||
size_a = len(first_term)
|
||||
size_b = len(second_term)
|
||||
second_term.reverse()
|
||||
|
||||
result: BasicRegister
|
||||
final_carry: bool
|
||||
result, final_carry = binary_sum(first_term, second_term, True)
|
||||
|
||||
if final_carry:
|
||||
return binary_sum(result, BasicRegister([True]))
|
||||
else:
|
||||
result.reverse()
|
||||
return result
|
||||
|
||||
|
||||
def align_registers(first_register: BasicRegister, second_register: BasicRegister) \
|
||||
-> tuple[BasicRegister, BasicRegister]:
|
||||
size_a = len(first_register)
|
||||
size_b = len(second_register)
|
||||
|
||||
required_size = max(size_a, size_b)
|
||||
|
||||
a = first_term
|
||||
b = second_term
|
||||
a = first_register
|
||||
b = second_register
|
||||
|
||||
if size_a != size_b:
|
||||
a = a.adjusted_by_size(required_size)
|
||||
b = b.adjusted_by_size(required_size)
|
||||
|
||||
b.reverse()
|
||||
|
||||
c: BasicRegister
|
||||
final_carry: bool
|
||||
c, final_carry = binary_sum(a, b, True)
|
||||
|
||||
if final_carry:
|
||||
return binary_sum(c, BasicRegister([True]))
|
||||
else:
|
||||
c.reverse()
|
||||
return c
|
||||
|
||||
|
||||
def binary_subtraction_2_complement(first_term: BasicRegister, second_term: BasicRegister) -> BasicRegister:
|
||||
pass
|
||||
return a, b
|
||||
|
|
16
main.py
16
main.py
|
@ -16,16 +16,16 @@ if __name__ == '__main__':
|
|||
print("Register 2:")
|
||||
print(reg2)
|
||||
|
||||
print()
|
||||
reg3: bu.BasicRegister = bu.binary_sum(reg, reg2)
|
||||
reg, reg2 = bu.align_registers(reg, reg2)
|
||||
|
||||
print()
|
||||
print("Sum:")
|
||||
print(reg3)
|
||||
|
||||
carry_sum_test: tuple[bu.BasicRegister, int] = bu.binary_sum(reg, reg2, True)
|
||||
sum_reg, carry = bu.binary_sum(reg, reg2, True)
|
||||
|
||||
print()
|
||||
print("Sum & carry:")
|
||||
# print(type(carry_sum_test))
|
||||
print(carry_sum_test)
|
||||
print(sum_reg, carry)
|
||||
|
||||
print()
|
||||
subtr_reg: bu.BasicRegister = bu.binary_subtraction_1_complement(reg, reg2)
|
||||
print("Subtraction:")
|
||||
print(subtr_reg)
|
||||
|
|
Loading…
Reference in New Issue