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