Fastest way to convert a byte of integer into a list

Godzilla godzillaismad at gmail.com
Thu Jul 12 23:05:31 EDT 2007


On Jul 13, 11:13 am, bsneddon <bsned... at yahoo.com> wrote:
> On Jul 12, 8:49 pm, John Machin <sjmac... at lexicon.net> wrote:
>
> > On Jul 13, 10:28 am, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
>
> > > Godzilla <godzillais... at gmail.com> writes:
> > > > > num = 255
> > > > > numlist = [num >> i & 1 for i in range(8)]
>
> > > > Thanks matimus! I will look into it...
>
> > > numlist = lookup_table[num]
>
> > > where lookup_table is a precomputed list of lists.
>
> > Ummm ... didn't the OP say he had 32-bit numbers???
>
> List comprehension would be faster, lookup would be even faster but
> would have to generate list or dictionary ahead of time
> but this will work on any length int up 2 limit of int does not pad
> with zeros on most significant end to word length.
>
> n=input()
> l=[]
> while(n>0):
>  l.append(str(n&1)); n=n>>1
>
> I posted this herehttp://www.uselesspython.com/download.php?script_id=222
> a while back.

Thanks all... I will have a look at it soon.

Regarding to the 32-bit number, the lenght is variable but it is
usually defined at design time...




More information about the Python-list mailing list