From 44457b1849bcf66ce1a7acfc48e47083cccc8741 Mon Sep 17 00:00:00 2001 From: dymik739 Date: Mon, 22 May 2023 22:23:55 +0300 Subject: [PATCH] bitutils.py: harden shifts, add multibit XOR operation and sum function with right alignment --- src/bitutils.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/bitutils.py b/src/bitutils.py index 7bd9654..aac0044 100644 --- a/src/bitutils.py +++ b/src/bitutils.py @@ -20,13 +20,13 @@ al = align_binary_to_left def shift_left(rg, fill_bit = 0): - return rg[1:] + fill_bit + return rg[1:] + str(fill_bit) l = shift_left def shift_right(rg, fill_bit = 0): - return fill_bit + rg[:-1] + return str(fill_bit) + rg[:-1] r = shift_right @@ -37,14 +37,20 @@ def sum_supplementary_codes(x, y, size): sum = sum_supplementary_codes -def sum_supplementary_codes_with_overspill(x, y, size): +def sum_supplementary_codes_right_align(x, y, size): + return ar(bin(int("0b"+x, 2) + int("0b"+y, 2))[2:], size) + +rsum = sum_supplementary_codes_right_align + + +def sum_supplementary_codes_with_overflow(x, y, size): result = bin(int("0b"+x, 2) + int("0b"+y, 2))[2:] if len(result) > size: return al(result, size), '1' else: return al(result, size), '0' -sump = sum_supplementary_codes_with_overspill +sump = sum_supplementary_codes_with_overflow def invert_bit(b): @@ -57,3 +63,15 @@ def invert_bit(b): exit(1) inv = invert_bit + + +def xor(x, y): + if len(x) == len(y): + result = '' + for i in zip(x, y): + if x != y: + result += '1' + else: + result += '0' + + return result