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
28 lines
424 B
def BintoInt(x):
|
|
i = int(x, base=2)
|
|
if x[0] == '1':
|
|
i = i - 2 ** len(x)
|
|
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:
|
|
x += 2 ** n
|
|
return UInttoBin(x, n)
|
|
|
|
|
|
def unsigned_ext(x, n):
|
|
return '0' * (n - len(x)) + x
|
|
|
|
|
|
def signed_ext(x, n):
|
|
return x[0] * (n - len(x)) + x
|