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)