Velocity Reviews > Fastest way to convert a byte of integer into a list

# Fastest way to convert a byte of integer into a list

Godzilla
Guest
Posts: n/a

 07-12-2007
Hello,

I'm trying to find a way to convert an integer (8-bits long for
starters) and converting them to a list, e.g.:

num = 255
numList = [1,1,1,1,1,1,1,1]

with the first element of the list being the least significant, so
that i can keep appending to that list without having to worry about
the size of the integer. I need to do this because some of the
function call can return a 2 lots of 32-bit numbers. I have to find a
way to transport this in a list... or is there a better way?

Matimus
Guest
Posts: n/a

 07-12-2007
On Jul 12, 3:34 pm, Godzilla <(E-Mail Removed)> wrote:
> Hello,
>
> I'm trying to find a way to convert an integer (8-bits long for
> starters) and converting them to a list, e.g.:
>
> num = 255
> numList = [1,1,1,1,1,1,1,1]
>
> with the first element of the list being the least significant, so
> that i can keep appending to that list without having to worry about
> the size of the integer. I need to do this because some of the
> function call can return a 2 lots of 32-bit numbers. I have to find a
> way to transport this in a list... or is there a better way?

num = 255
numlist = [num >> i & 1 for i in range(]

Godzilla
Guest
Posts: n/a

 07-13-2007
On Jul 13, 9:54 am, Matimus <(E-Mail Removed)> wrote:
> On Jul 12, 3:34 pm, Godzilla <(E-Mail Removed)> wrote:
>
> > Hello,

>
> > I'm trying to find a way to convert an integer (8-bits long for
> > starters) and converting them to a list, e.g.:

>
> > num = 255
> > numList = [1,1,1,1,1,1,1,1]

>
> > with the first element of the list being the least significant, so
> > that i can keep appending to that list without having to worry about
> > the size of the integer. I need to do this because some of the
> > function call can return a 2 lots of 32-bit numbers. I have to find a
> > way to transport this in a list... or is there a better way?

>
> num = 255
> numlist = [num >> i & 1 for i in range(]

Thanks matimus! I will look into it...

Paul Rubin
Guest
Posts: n/a

 07-13-2007
Godzilla <(E-Mail Removed)> writes:
> > num = 255
> > numlist = [num >> i & 1 for i in range(]

>
> Thanks matimus! I will look into it...

numlist = lookup_table[num]

where lookup_table is a precomputed list of lists.

Alan Isaac
Guest
Posts: n/a

 07-13-2007
> On Jul 13, 9:54 am, Matimus <(E-Mail Removed)> wrote:
>>num = 255
>>numlist = [num >> i & 1 for i in range(]

Godzilla wrote:
> Thanks matimus! I will look into it...

Watch out for the order, which might
or might not match your intent.

Cheers,
Alan Isaac

John Machin
Guest
Posts: n/a

 07-13-2007
On Jul 13, 10:28 am, Paul Rubin <http://(E-Mail Removed)> wrote:
> Godzilla <(E-Mail Removed)> writes:
> > > num = 255
> > > numlist = [num >> i & 1 for i in range(]

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

Paul Rubin
Guest
Posts: n/a

 07-13-2007
John Machin <(E-Mail Removed)> writes:
> > numlist = lookup_table[num]
> > where lookup_table is a precomputed list of lists.

> Ummm ... didn't the OP say he had 32-bit numbers???

but figured those would be split into bytes or something:
(untested and I don't remember if this is the byte order wanted):

from itertools import chain
numlist = list(chain(lookup_table[(num>>i)&0xff] for i in xrange(0,32,))

bsneddon
Guest
Posts: n/a

 07-13-2007
On Jul 12, 8:49 pm, John Machin <(E-Mail Removed)> wrote:
> On Jul 13, 10:28 am, Paul Rubin <http://(E-Mail Removed)> wrote:
>
> > Godzilla <(E-Mail Removed)> writes:
> > > > num = 255
> > > > numlist = [num >> i & 1 for i in range(]

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

a while back.

Godzilla
Guest
Posts: n/a

 07-13-2007
On Jul 13, 11:13 am, bsneddon <(E-Mail Removed)> wrote:
> On Jul 12, 8:49 pm, John Machin <(E-Mail Removed)> wrote:
>
> > On Jul 13, 10:28 am, Paul Rubin <http://(E-Mail Removed)> wrote:

>
> > > Godzilla <(E-Mail Removed)> writes:
> > > > > num = 255
> > > > > numlist = [num >> i & 1 for i in range(]

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

Paul Rubin
Guest
Posts: n/a

 07-13-2007
Godzilla <(E-Mail Removed)> writes:
> Regarding to the 32-bit number, the lenght is variable but it is
> usually defined at design time...

That you're trying to represent it as a list of bits at all is
weird, and probably likely to slow the program down. You do know
that python has arbitrary sized ints, right?