From f477c6f9e166414ebe5ddcfc1d7e073e6cca5950 Mon Sep 17 00:00:00 2001 From: Rhinemann Date: Tue, 27 Jun 2023 15:34:39 +0300 Subject: [PATCH] binary_subtraction_1_complement initial commit. --- bitutilities.py | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/bitutilities.py b/bitutilities.py index e8179fe..8702e0d 100644 --- a/bitutilities.py +++ b/bitutilities.py @@ -68,7 +68,7 @@ def get_memory(variable_name: str) -> list[bool]: def binary_sum(first_term: BasicRegister, second_term: BasicRegister, return_remainder: bool = False)\ - -> BasicRegister | tuple[BasicRegister, int]: + -> BasicRegister or tuple[BasicRegister, int]: """ Sums two registers' values. @@ -103,6 +103,37 @@ def binary_sum(first_term: BasicRegister, second_term: BasicRegister, return_rem c.memory[0] = bool(final_bit_sum & 1) if return_remainder: - return c, carry + final_carry = bool(final_bit_sum & 2) + return c, final_carry else: return c + + +def binary_subtraction_1_complement(first_term: BasicRegister, second_term: BasicRegister) -> BasicRegister: + 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) + + 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