From 5a26802c341703222ba3648099c96b7a5dad2fbd Mon Sep 17 00:00:00 2001 From: dymik739 Date: Tue, 27 Jun 2023 22:09:45 +0300 Subject: [PATCH] optimize binary_sum_with_carry function by removing code repetition --- bitutilities.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/bitutilities.py b/bitutilities.py index 33a9986..f779880 100644 --- a/bitutilities.py +++ b/bitutilities.py @@ -127,16 +127,12 @@ def binary_sum_with_carry(first_term: BasicRegister, second_term: BasicRegister) result_term = BasicRegister([False] * len(first_term)) carry = False - for i in range(len(first_term) - 1, 0, -1): + for i in range(len(first_term) - 1, -1, -1): current_bit_sum = first_term.memory[i] + second_term.memory[i] + carry carry = bool(current_bit_sum & 2) result_term.memory[i] = bool(current_bit_sum & 1) - final_bit_sum = first_term.memory[0] + second_term.memory[0] + carry - final_carry = bool(final_bit_sum & 2) - result_term.memory[0] = bool(final_bit_sum & 1) - - return result_term, final_carry + return result_term, carry def binary_sum(first_term: BasicRegister, second_term: BasicRegister) -> BasicRegister: