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.
19 lines
300 B
19 lines
300 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
|
||
|
return UInttoBin(x, n)
|