optimize binary_sum_with_carry function by removing code repetition

This commit is contained in:
dymik739 2023-06-27 22:09:45 +03:00
parent 9925c22954
commit 5a26802c34
1 changed files with 2 additions and 6 deletions

View File

@ -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: