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: 
Here is one method for decimal to binary conversion:

* divide the decimal number by 2
* take the remainder and record it in on the side
* divide the quotient by 2
* repeat until the decimal cannot be divided further
* record the remainder in reverse order and you get the resultant binary number

Which may be coded as: 

```
d=int(raw_input("enter your decimal:"))
l=[]
while d>0:
    x=d%2
    l.append(x)
    d=d/2
l.reverse()
for i in l:
    print i,
print " is the decimal representation of givin binary data."

```
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

```