Question: I need to convert from an integer to a list of size 8 that is the binary representation of that number (number <= 255) and back. Currently I am using these lines

```
list(bin(my_num)[2:].rjust(8,'0'))
int("".join(my_list),2)

```

I did some googling, but had a hard time finding relevant information. I'm just curious if there is a faster, or more standard way to do this.

**edit:**
Would using bit masking make it faster. E.g. something like this

`[(my_num>>y)&1 for y in xrange(7,-1,-1)]`

Like I mentioned in a comment I am using this for a steganography app I am writing, so I am doing this thousands of times (3 times per pixel in an image), so speed is good.

Answer: 
You could use [`zfill`](http://docs.python.org/library/stdtypes.html#str.zfill) instead of `rjust`.

```
list(bin(my_num)[2:].zfill(8))

```
I have given here program for decimal to binary conversion.

```
print "Program for Decimal to Binary Conversion"

n = 0
bin = 0
pos = 1

print "Enter Decimal Number:", 
n = input()

while(n > 0):
   bin = bin + (n % 2) * pos;
   n = n / 2;
   pos *= 10;

print "The Binary Number is: ", bin       

#sample output
#Program for Decimal to Binary Conversion
#Enter Decimal Number: 10
#The Binary Number is: 1010

```