Compare commits

..

2 Commits

Author SHA1 Message Date
Rhinemann 0f94f6d37a Minor rewrites. 2023-06-27 16:43:18 +03:00
Rhinemann 9fedc98310 Implemented input_handler function. 2023-06-27 16:40:09 +03:00
2 changed files with 35 additions and 37 deletions

View File

@ -97,7 +97,8 @@ def binary_sum(first_term: BasicRegister, second_term: BasicRegister, return_rem
return result
def binary_subtraction_1_complement(first_term: BasicRegister, second_term: BasicRegister) -> BasicRegister:
def binary_subtraction(first_term: BasicRegister, second_term: BasicRegister) -> BasicRegister:
second_term.reverse()
result: BasicRegister
@ -105,7 +106,7 @@ def binary_subtraction_1_complement(first_term: BasicRegister, second_term: Basi
result, final_carry = binary_sum(first_term, second_term, True)
if final_carry:
return binary_sum(result, BasicRegister([True]))
return binary_sum(result, BasicRegister([True] * len(result)))
else:
result.reverse()
return result
@ -113,16 +114,12 @@ def binary_subtraction_1_complement(first_term: BasicRegister, second_term: Basi
def align_registers(first_register: BasicRegister, second_register: BasicRegister) \
-> tuple[BasicRegister, BasicRegister]:
size_a = len(first_register)
size_b = len(second_register)
first_size = len(first_register)
second_size = len(second_register)
required_size = max(size_a, size_b)
required_size = max(first_size, second_size)
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)
return a, b
if first_size != second_size:
return first_register.adjusted_by_size(required_size), second_register.adjusted_by_size(required_size)
else:
return first_register, second_register

49
main.py
View File

@ -1,31 +1,32 @@
import bitutilities as bu
import timeit
def input_handler(first_register: bu.BasicRegister, second_register: bu.BasicRegister):
first_register, second_register = bu.align_registers(first_register, second_register)
print()
print(first_register)
print(second_register)
while True:
print()
match input("Choose the operation:\n[a]ddition, [s]ubtraction, [m]ultiplication, [d]ivision, [q]uit\n>>> "):
case "a":
print(f"Sum:\n{bu.binary_sum(first_register, second_register)}")
case "s":
print(f"Subtraction:\n{bu.binary_subtraction(first_register, second_register)}")
case "m":
pass
case "d":
pass
case "q":
exit()
case _:
print("Not an available operation, try again.")
if __name__ == '__main__':
reg: bu.BasicRegister = bu.BasicRegister(bu.get_memory("memory"))
print()
print("Register 1:")
print(reg)
print()
reg2: bu.BasicRegister = bu.BasicRegister(bu.get_memory("more memory"))
print()
print("Register 2:")
print(reg2)
reg, reg2 = bu.align_registers(reg, reg2)
print()
sum_reg, carry = bu.binary_sum(reg, reg2, True)
print()
print("Sum & carry:")
print(sum_reg, carry)
print()
subtr_reg: bu.BasicRegister = bu.binary_subtraction_1_complement(reg, reg2)
print("Subtraction:")
print(subtr_reg)
input_handler(reg, reg2)