Converting integer to binary representation

Anton Vredegoor anton at vredegoor.doge.nl
Wed Dec 17 07:30:36 EST 2003


Mark Dufour <m.dufour at student.tudelft.nl> wrote:

>I want to loop over the binary representation of numbers, for use in some 
>algorithm dealing with prime numbers. Of course, there are multiple ways of 
>doing this, but I like the clean: 'for power, bit in 
>enumerate(binary(number))', possible with the above code. 

Currently strings are immutable sequences of characters. So it would
be possible to do what you write above if integers where seen as
sequences of boolean in the same way. I guess it's obvious if you're
Dutch:

>>> list(10)
>>> [True,False,True,False]

Anton




More information about the Python-list mailing list