cl-tools/src/bitutils.py

40 lines
711 B
Python
Raw Normal View History

2023-04-03 23:26:59 +03:00
def align_binary_to_right(value, size):
if "b" in value:
result = value.split("b")[1]
else:
result = str(value)
return result[-size:].rjust(size, "0")
al = align_binary_to_right
def shift_left(rg, fill_bit = 0):
return rg[1:] + fill_bit
l = shift_left
def shift_right(rg, fill_bit = 0):
return fill_bit + rg[:-1]
r = shift_right
def sum_supplementary_codes(x, y, size):
return al(bin(int("0b"+x, 2) + int("0b"+y, 2))[2:], size)
sum = sum_supplementary_codes
def invert_bit(b):
if b == '0':
return '1'
elif b == '1':
return '0'
else:
print(f"binutils: detected impossible call: inv({b})")
exit(1)
inv = invert_bit