Compare commits
No commits in common. "0f94f6d37a34d20fb9f06d925173c066e7ef89bf" and "b3c5011c114ea280e3faeb84076ecd356fb10fe5" have entirely different histories.
0f94f6d37a
...
b3c5011c11
|
@ -97,8 +97,7 @@ def binary_sum(first_term: BasicRegister, second_term: BasicRegister, return_rem
|
|||
return result
|
||||
|
||||
|
||||
def binary_subtraction(first_term: BasicRegister, second_term: BasicRegister) -> BasicRegister:
|
||||
|
||||
def binary_subtraction_1_complement(first_term: BasicRegister, second_term: BasicRegister) -> BasicRegister:
|
||||
second_term.reverse()
|
||||
|
||||
result: BasicRegister
|
||||
|
@ -106,7 +105,7 @@ def binary_subtraction(first_term: BasicRegister, second_term: BasicRegister) ->
|
|||
result, final_carry = binary_sum(first_term, second_term, True)
|
||||
|
||||
if final_carry:
|
||||
return binary_sum(result, BasicRegister([True] * len(result)))
|
||||
return binary_sum(result, BasicRegister([True]))
|
||||
else:
|
||||
result.reverse()
|
||||
return result
|
||||
|
@ -114,12 +113,16 @@ def binary_subtraction(first_term: BasicRegister, second_term: BasicRegister) ->
|
|||
|
||||
def align_registers(first_register: BasicRegister, second_register: BasicRegister) \
|
||||
-> tuple[BasicRegister, BasicRegister]:
|
||||
first_size = len(first_register)
|
||||
second_size = len(second_register)
|
||||
size_a = len(first_register)
|
||||
size_b = len(second_register)
|
||||
|
||||
required_size = max(first_size, second_size)
|
||||
required_size = max(size_a, size_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
|
||||
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
|
||||
|
|
49
main.py
49
main.py
|
@ -1,32 +1,31 @@
|
|||
import bitutilities as bu
|
||||
|
||||
|
||||
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.")
|
||||
import timeit
|
||||
|
||||
|
||||
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"))
|
||||
|
||||
input_handler(reg, reg2)
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue