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.

27 lines
419 B

3 years ago
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
3 years ago
return UInttoBin(x, n)
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