bitutils.py: harden shifts, add multibit XOR operation and sum function with right alignment

This commit is contained in:
dymik739 2023-05-22 22:23:55 +03:00
parent 31ec6b2697
commit 44457b1849
1 changed files with 22 additions and 4 deletions

View File

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