473,795 Members | 2,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fastest way to convert a byte of integer into a list

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?

Jul 12 '07 #1
12 5072
On Jul 12, 3:34 pm, Godzilla <godzillais...@ gmail.comwrote:
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(8)]

Jul 12 '07 #2
On Jul 13, 9:54 am, Matimus <mccre...@gmail .comwrote:
On Jul 12, 3:34 pm, Godzilla <godzillais...@ gmail.comwrote:
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(8)]
Thanks matimus! I will look into it...

Jul 13 '07 #3
Godzilla <go***********@ gmail.comwrites :
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.
Jul 13 '07 #4
On Jul 13, 10:28 am, Paul Rubin <http://phr...@NOSPAM.i nvalidwrote:
Godzilla <godzillais...@ gmail.comwrites :
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???

Jul 13 '07 #5
On Jul 12, 8:49 pm, John Machin <sjmac...@lexic on.netwrote:
On Jul 13, 10:28 am, Paul Rubin <http://phr...@NOSPAM.i nvalidwrote:
Godzilla <godzillais...@ gmail.comwrites :
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 here http://www.uselesspython.com/download.php?script_id=222
a while back.

Jul 13 '07 #6
On Jul 13, 11:13 am, bsneddon <bsned...@yahoo .comwrote:
On Jul 12, 8:49 pm, John Machin <sjmac...@lexic on.netwrote:
On Jul 13, 10:28 am, Paul Rubin <http://phr...@NOSPAM.i nvalidwrote:
Godzilla <godzillais...@ gmail.comwrites :
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.uselesspyth on.com/download.php?sc ript_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...

Jul 13 '07 #7
Godzilla <go***********@ gmail.comwrites :
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?
Jul 13 '07 #8
On Jul 12, 5:34 pm, Godzilla <godzillais...@ gmail.comwrote:
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?
Standing on the shoulders of previous posters, I put this together.

-- Paul
# init list of tuples by byte
bytebits = lambda num : [num >i & 1 for i in range(8)]
bytes = [ tuple(bytebits( i)) for i in range(256) ]

# use bytes lookup to get bits in a 32-bit integer
bits = lambda num : sum((bytes[num >i & 255] for i in range(0,32,8)),
())

# use base-2 log to find how many bits in an integer of arbitrary
length
from math import log,ceil
log_of_2 = log(2)
numBits = lambda num : int(ceil(log(nu m)/log_of_2))

# expand bits to integers of arbitrary length
arbBits = lambda num : sum((bytes[num >i & 255] for i in
range(0,numBits (num),8)),())

print arbBits((1<<34)-1)
print arbBits(37)

# prints
#(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0)
#(1, 0, 1, 0, 0, 1, 0, 0)

Jul 13 '07 #9
On Jul 13, 5:17 am, Paul McGuire <pt...@austin.r r.comwrote:
On Jul 12, 5:34 pm, Godzilla <godzillais...@ gmail.comwrote:
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?

Standing on the shoulders of previous posters, I put this together.

-- Paul
But aren't we moving backwards? The OP did ask for the fastest way.

I put this together (from other posters and my own):

import gmpy
import time

y = 2**177149 - 1

# init list of tuples by byte
bytebits = lambda num : [num >i & 1 for i in range(8)]
bytes = [ tuple(bytebits( i)) for i in range(256) ]
# use bytes lookup to get bits in a 32-bit integer
bits = lambda num : sum((bytes[num >i & 255] for i in range(0,32,8)),
())
# use base-2 log to find how many bits in an integer of arbitrary
length
from math import log,ceil
log_of_2 = log(2)
numBits = lambda num : int(ceil(log(nu m)/log_of_2))
# expand bits to integers of arbitrary length
arbBits = lambda num : sum((bytes[num >i & 255] for i in
range(0,numBits (num),8)),())
t0 = time.time()
L = arbBits(y)
t1 = time.time()
print 'Paul McGuire algorithm:',t1-t0

t0 = time.time()
L = [y >i & 1 for i in range(177149)]
t1 = time.time()
print ' Matimus algorithm:',t1-t0

x = gmpy.mpz(2**177 149 - 1)
t0 = time.time()
L = [gmpy.getbit(x,i ) for i in range(177149)]
t1 = time.time()
print ' Mensanator algorithm:',t1-t0

## Paul McGuire algorithm: 17.4839999676
## Matimus algorithm: 3.28100013733
## Mensanator algorithm: 0.125

Jul 13 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
2334
by: DraguVaso | last post by:
Hi, I need to find the FASTEST way to get a string in a Loop, that goes from "a" to "ZZZZZZZZZZZZZZZZZ". So it has to go like this: a b .... z
6
5494
by: Jonathan | last post by:
I am hoping that someone more experienced than myself can point me towards what might be the fastest data lookup method to use for storing ip addresses. My situation is that I will need to maintain a list of perhaps 50 ip addresses to be used in a packet sniffing application. For each packet that goes through the application (which will be monitoring all traffic through a switch), I need to see if an entry for the source ip of that packet...
5
716
by: Daniel | last post by:
in C# fastest way to convert a string into a MemoryStream
9
12689
by: Charles Law | last post by:
Suppose I have a structure Private Structure MyStruct Dim el1 As Byte Dim el2 As Int16 Dim el3 As Byte End Structure I want to convert this into a byte array where
14
29730
by: Charles Law | last post by:
I thought this had come up before, but I now cannot find it. I have a byte array, such as Dim a() As Byte = {1, 2, 3, 4} I want to convert this to an Int32 = 01020304 (hex). If I use BitConverter, I get 04030201.
6
53725
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a string, and then from a string back to a byte array. for example Private mByte() as New Byte(4){11,22,33,44} Now how do I convert it to: dim myStr as string = "11,22,33,44"
5
8652
by: Bob Homes | last post by:
In VB6, foreground and background colors of controls had to be assigned a single number. If you knew the RGB values for the color, you still had to convert them into the single number accepatable to the VB6 controls. In VB.NET, you can't set colors that way anymore, now you have to use a "color". There is a way to convert RGB values to a "color", using the Color.FromARGB method. But there doens't seem to be a way to convert the old...
24
2294
by: ThunderMusic | last post by:
Hi, The subject says it all... I want to use a byte and use it as byte* so I can increment the pointer to iterate through it. What is the fastest way of doing so in C#? Thanks ThunderMusic
0
1444
by: laviewpbt | last post by:
Fist ,Add a new class named "CImage",then copythe following codes and paste to it. Option Explicit Private Type BITMAPFILEHEADER bfType As Integer bfSize As Long bfReserved1 As Integer
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10443
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9044
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7543
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.