From f2ee4cd7092e9e9a4b3d9f3daed651db7a8055af Mon Sep 17 00:00:00 2001 From: dymik739 Date: Mon, 26 Jun 2023 22:33:41 +0300 Subject: [PATCH] fix BasicRegister shrinking --- bitutilities.py | 4 ++-- main.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bitutilities.py b/bitutilities.py index 7cac662..5a015f5 100644 --- a/bitutilities.py +++ b/bitutilities.py @@ -27,7 +27,7 @@ class BasicRegister: """ current_memory_size: int = len(self.memory) return BasicRegister( - [False] * max(resulting_size - current_memory_size, 0) + list(self.memory)[-current_memory_size:] + [False] * max(resulting_size - current_memory_size, 0) + list(self.memory)[-resulting_size:] ) def reverse(self): @@ -87,7 +87,7 @@ def binary_sum(a_original: BasicRegister, b_original: BasicRegister) -> BasicReg c = BasicRegister([False] * required_size) carry = False - for i in range(size_a - 1, 0, -1): + for i in range(required_size - 1, 0, -1): current_bit_sum = a.memory[i] + b.memory[i] + carry carry = bool(current_bit_sum & 2) c.memory[i] = bool(current_bit_sum & 1) diff --git a/main.py b/main.py index d1515d5..bca254b 100644 --- a/main.py +++ b/main.py @@ -23,6 +23,6 @@ if __name__ == '__main__': reg2: BasicRegister = BasicRegister(get_memory("more memory")) - reg3: BasicRegister = sum(reg, reg2) + reg3: BasicRegister = binary_sum(reg, reg2) print(reg3)