You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
424 B

3 years ago
def BintoInt(x):
i = int(x, base=2)
if x[0] == '1':
3 years ago
i = i - 2 ** len(x)
3 years ago
return i
def BintoUInt(x):
return int(x, base=2)
def UInttoBin(x, n):
return '0' * (n - len(bin(x)[2:])) + bin(x)[2:]
def InttoBin(x, n):
if x < 0:
3 years ago
x += 2 ** n
3 years ago
return UInttoBin(x, n)
3 years ago
3 years ago
3 years ago
def unsigned_ext(x, n):
return '0' * (n - len(x)) + x
def signed_ext(x, n):
return x[0] * (n - len(x)) + x