An expression like map(int, list(bin((1<<8)+x))[-4:]) will give you 4 bits of a number in the form of a list. (Edit: a cleaner map(int,bin(x)[2:].zfill(4)) form map(int,bin(x)[2:].zfill(4)) , see below.) If you know how many bits you want to show, replace 4 (in [-4:] ) on this number; and if necessary 8 (in (1<<8) ) increase the number. For instance:
>>> x=0b0111 >>> map(int,list(bin((1<<8)+x))[-4:]) [0, 1, 1, 1] >>> x=37; map(int,list(bin((1<<8)+x))[-7:]) [0, 1, 0, 0, 1, 0, 1] >>> [int(d) for d in bin((1<<8)+x)[-7:]] [0, 1, 0, 0, 1, 0, 1]
The final example above shows an alternative to using a map and list. The following examples show a slightly cleaner form for leading zeros. In these forms, replace the required minimum number of bits instead of 8.
>>> x=37; [int(d) for d in bin(x)[2:].zfill(8)] [0, 0, 1, 0, 0, 1, 0, 1] >>> x=37; map(int,bin(x)[2:].zfill(8)) [0, 0, 1, 0, 0, 1, 0, 1] >>> x=37; map(int,bin(x)[2:].zfill(5)) [1, 0, 0, 1, 0, 1] >>> x=37; map(lambda k:(x>>-k)&1, range(-7,1)) [0, 0, 1, 0, 0, 1, 0, 1]