From d2df4bbf2852ec4c1b3409c859bc36e9c54b78fc Mon Sep 17 00:00:00 2001 From: Rhinemann Date: Tue, 27 Jun 2023 14:18:10 +0300 Subject: [PATCH] Renamed arguments inside binary_sum. Added carry return (VERY NOT SURE IF IT WORKS CORRECTLY). --- bitutilities.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/bitutilities.py b/bitutilities.py index c586d9d..e8179fe 100644 --- a/bitutilities.py +++ b/bitutilities.py @@ -67,23 +67,25 @@ 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(a_original: BasicRegister, b_original: BasicRegister) -> BasicRegister: +def binary_sum(first_term: BasicRegister, second_term: BasicRegister, return_remainder: bool = False)\ + -> BasicRegister | tuple[BasicRegister, int]: """ Sums two registers' values. - :param BasicRegister a_original: First register. - :param BasicRegister b_original: Second register. + :param BasicRegister first_term: First register. + :param BasicRegister second_term: Second register. + :param bool return_remainder: True to return the tuple, False to return just the register. - :return: Register containing the sum. - :rtype: BasicRegister + :return: Register containing the sum or the tuple containing the register and carried radix. + :rtype: BasicRegister | tuple[BasicRegister, int] """ - size_a = len(a_original) - size_b = len(b_original) + size_a = len(first_term) + size_b = len(second_term) required_size = max(size_a, size_b) - a = a_original - b = b_original + a = first_term + b = second_term if size_a != size_b: a = a.adjusted_by_size(required_size) @@ -100,4 +102,7 @@ def binary_sum(a_original: BasicRegister, b_original: BasicRegister) -> BasicReg final_bit_sum = a.memory[0] + b.memory[0] + carry c.memory[0] = bool(final_bit_sum & 1) - return c + if return_remainder: + return c, carry + else: + return c