bitutils.py: harden shifts, add multibit XOR operation and sum function with right alignment
This commit is contained in:
parent
31ec6b2697
commit
44457b1849
|
@ -20,13 +20,13 @@ al = align_binary_to_left
|
||||||
|
|
||||||
|
|
||||||
def shift_left(rg, fill_bit = 0):
|
def shift_left(rg, fill_bit = 0):
|
||||||
return rg[1:] + fill_bit
|
return rg[1:] + str(fill_bit)
|
||||||
|
|
||||||
l = shift_left
|
l = shift_left
|
||||||
|
|
||||||
|
|
||||||
def shift_right(rg, fill_bit = 0):
|
def shift_right(rg, fill_bit = 0):
|
||||||
return fill_bit + rg[:-1]
|
return str(fill_bit) + rg[:-1]
|
||||||
|
|
||||||
r = shift_right
|
r = shift_right
|
||||||
|
|
||||||
|
@ -37,14 +37,20 @@ def sum_supplementary_codes(x, y, size):
|
||||||
sum = sum_supplementary_codes
|
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:]
|
result = bin(int("0b"+x, 2) + int("0b"+y, 2))[2:]
|
||||||
if len(result) > size:
|
if len(result) > size:
|
||||||
return al(result, size), '1'
|
return al(result, size), '1'
|
||||||
else:
|
else:
|
||||||
return al(result, size), '0'
|
return al(result, size), '0'
|
||||||
|
|
||||||
sump = sum_supplementary_codes_with_overspill
|
sump = sum_supplementary_codes_with_overflow
|
||||||
|
|
||||||
|
|
||||||
def invert_bit(b):
|
def invert_bit(b):
|
||||||
|
@ -57,3 +63,15 @@ def invert_bit(b):
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
inv = invert_bit
|
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
|
||||||
|
|
Loading…
Reference in New Issue