I am currently in the process of porting some C code into Python and am
stuck. I don't claim to be the greatest C/C++ programmer; in fact, my
skills at C are rudimentary at best. My question is I have the
statement: "typedef unsigned long int word32" and later on: "word32
b[3]" referencing the third bit of the integer. How do I do the same in
Python?? If this is a stupid, I apologize. It's amazing what the lack of
sleep does to the brain.
TIA,
Lucas 8 2031
Lucas Raab wrote: I am currently in the process of porting some C code into Python and am stuck. I don't claim to be the greatest C/C++ programmer; in fact, my skills at C are rudimentary at best. My question is I have the statement: "typedef unsigned long int word32" and later on: "word32 b[3]" referencing the third bit of the integer. How do I do the same in Python??
py> for x in range(16):
.... print x, (x >> 2) & 1
....
0 0
1 0
2 0
3 0
4 1
5 1
6 1
7 1
8 0
9 0
10 0
11 0
12 1
13 1
14 1
15 1
Basically, I use a right-shift by 2 to put the 3rd bit as the last bit,
and then mask off everything but the last bit by and-ing with 1. Does
that work?
Steve
Lucas Raab <py*********@ho tmail.com> wrote: I am currently in the process of porting some C code into Python and am stuck. I don't claim to be the greatest C/C++ programmer; in fact, my skills at C are rudimentary at best. My question is I have the statement: "typedef unsigned long int word32" and later on: "word32 b[3]" referencing the third bit of the integer.
Are you sure that's accessing the third bit? It looks to me like it's
accessing index 3 (counting from 0) of an array of ints.
How do I do the same in Python??
In any case, if you're doing bit-fiddling in Python, you've got two
basic sets of tools.
First, the basic arithmetic operators. Boolean and (&), or (|),
left-shift (<<) and right-shift (>>) operators work in Python just like
they do in C. Integer constants starting with 0x are in hex, and the
"%x" format specifier prints integers in hex. You can play all the
normal C bit-operation tricks. To test bit 7, you can do "word & (0x1
<< 7)". To set bit 4, use "word |= (0x1 << 4)".
Second, the struct module
( http://docs.python.org/lib/module-struct.html) is useful for packing
and unpacking C structures written out into binary files.
Lucas Raab wrote: I have the statement: "typedef unsigned long int word32" and later on: "word32 b[3]" referencing the third bit of the integer.
If that's really exactly what you have, then you actually have
something defining an array of three unsigned long integers
named "b". And even if you didn't have precisely "word32 b[3]",
but merely a "b[3]" reference somewhere, it would be referencing
the third element of an array called "b", which is possibly a byte,
maybe a long, but definitely not a bit.
Maybe showing it as code rather than inline in your text
would avoid the possibility of confusion.
-Peter
Peter Hansen wrote: but merely a "b[3]" reference somewhere, it would be referencing the third element of an array called "b", which is possibly a byte,
"*Fourth* element... I'll come in again. Amongst our elements..."
-Peter
Peter Hansen wrote: Lucas Raab wrote:
I have the statement: "typedef unsigned long int word32" and later on: "word32 b[3]" referencing the third bit of the integer.
If that's really exactly what you have, then you actually have something defining an array of three unsigned long integers named "b". And even if you didn't have precisely "word32 b[3]", but merely a "b[3]" reference somewhere, it would be referencing the third element of an array called "b", which is possibly a byte, maybe a long, but definitely not a bit.
Maybe showing it as code rather than inline in your text would avoid the possibility of confusion.
-Peter
Sorry, the third "byte" is what I meant. As for code samples, I hope the
following will work:
typedef unsigned long int word32 ;
void mu(word32 *a)
{
int i ;
word32 b[3] ;
b[0] = b[1] = b[2] = 0 ;
for( i=0 ; i<32 ; i++ )
{
b[0] <<= 1 ; b[1] <<= 1 ; b[2] <<= 1 ;
if(a[0]&1) b[2] |= 1 ;
if(a[1]&1) b[1] |= 1 ;
if(a[2]&1) b[0] |= 1 ;
a[0] >>= 1 ; a[1] >>= 1 ; a[2] >>= 1 ;
}
a[0] = b[0] ; a[1] = b[1] ; a[2] = b[2] ;
}
The "a[#]" and "b[#]" are the parts that are giving me trouble.
Lucas Raab wrote: Sorry, the third "byte" is what I meant. As for code samples, I hope the following will work:
typedef unsigned long int word32 ; void mu(word32 *a) { int i ; word32 b[3] ;
b[0] = b[1] = b[2] = 0 ; for( i=0 ; i<32 ; i++ ) { b[0] <<= 1 ; b[1] <<= 1 ; b[2] <<= 1 ; if(a[0]&1) b[2] |= 1 ; if(a[1]&1) b[1] |= 1 ; if(a[2]&1) b[0] |= 1 ; a[0] >>= 1 ; a[1] >>= 1 ; a[2] >>= 1 ; }
a[0] = b[0] ; a[1] = b[1] ; a[2] = b[2] ; }
The "a[#]" and "b[#]" are the parts that are giving me trouble.
So far as I can tell, the function takes an array of 3 32bit values,
reverses the order of the bits in each value, and swaps the first and last
elements of the array. All of this seems to be being done in an attempt to
make the process as obscure and inefficient as possible both by using
meaningless names, and by doing both operations simultaneously.
To convert this to Python I might try something like:
rev2 = [ 0, 0x02, 0x01, 0x03 ]
rev4 = [ (lo|hi<<2) for lo in rev2 for hi in rev2 ]
rev8 = [ (lo|hi<<4) for lo in rev4 for hi in rev4 ]
def rev32(n):
'''Reverse the low 32bits of an integer'''
return (rev8[(n>>24)&0xff]|
(rev8[(n>>16)&0xff]<<8)|
(rev8[(n>>8)&0xff]<<16)|
(rev8[n&0xff]<<24))
def mu(a):
'''Reverse the bit order of a list of 32bit integers while
also reversing the list itself'''
return [rev32(n) for n in reversed(a)]
print [hex(n) for n in mu([0x10203040, 0x50607080, 0x90a0b0c0])]
Although if 'a' really is just being used as a 96 bit integer I could add a
96 bit variant of the reversal function and use it directly:
def rev96(n):
'''Reverse the low 96bits of an integer'''
return rev32(n>>64)|(r ev32(n>>32)<<32 )|(rev32(n)<<64 )
print hex(rev96(0x102 030405060708090 a0b0c0L))
Lucas Raab wrote: Sorry, the third "byte" is what I meant.
Fair enough. Note, however, that as someone pointed out,
it's actually the *fourth* of something, and it would not
necessarily be a byte. In fact, in your case, it's not:
typedef unsigned long int word32 ; void mu(word32 *a) { int i ; word32 b[3] ;
This defines an array of *3* long (32-bit) integers.
b[0] = b[1] = b[2] = 0 ;
Each of these is just indexing into that array, starting
as Python does with an index origin of zero.
The "a[#]" and "b[#]" are the parts that are giving me trouble.
Between the clarifications you've got and Duncan's post,
you shouldn't have much more trouble now. :-)
-Peter
Peter Hansen wrote: Lucas Raab wrote:
Sorry, the third "byte" is what I meant.
Fair enough. Note, however, that as someone pointed out, it's actually the *fourth* of something, and it would not necessarily be a byte. In fact, in your case, it's not:
typedef unsigned long int word32 ; void mu(word32 *a) { int i ; word32 b[3] ;
This defines an array of *3* long (32-bit) integers.
b[0] = b[1] = b[2] = 0 ;
Each of these is just indexing into that array, starting as Python does with an index origin of zero.
The "a[#]" and "b[#]" are the parts that are giving me trouble.
Between the clarifications you've got and Duncan's post, you shouldn't have much more trouble now. :-)
-Peter
Thanks. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: eichin |
last post by:
One of my recent projects has involved taking an accretion of sh and
perl scripts and "doing them right" - making them modular, improving
the error reporting, making it easier to add even more features to
them. "Of course," I'm redoing them in python - much of the cut&paste
reuse has become common functions, which then get made more robust and
have a common style and are callable from other (python) tools
directly, instead of having to...
|
by: Anand |
last post by:
Hi
Are there any tools that would help in porting code from
Pyton 2.3 to 2.4 ? I have gone through the whatsnew documents
and created a document comparing Python 2.4 to 2.3. But so far
has not been able to find any tool that will signal code in
Python 2.3 that can cause errors in Python 2.4 .
rgds
|
by: Sonny |
last post by:
I need to port a library that is written entirely in C to C++. The
library is supported on quite a few platforms (windows, Solaris,
Linux, AIX, HP-UX, OSX, etc...) and there's quite an existing customer
base that uses it. I need to maintain backwards compatibility such
that existing users won't have to do anything to their existing
applications other than a re-compile when they upgrade to this new
version of the library. I figure that I...
|
by: Ryan Liu |
last post by:
Hi All,
Now I am porting CC to GCC and I have some problems.
Would you mind tell me some document which have some description how to port
CC to GCC ??
Thank you very much.
Ryan
|
by: Chris Travers |
last post by:
Hi all;
A few years ago, I set about porting a PHP application from MySQL to
PostgreSQL, after realizing that MySQL wasn't going to be able to handle it.
In order to do this, I built a light, fast database abstraction layer which
conforms to the behavior of the MySQL functions in PHP. This means that a
large amount of porting work could be made simple using this porting layer
if the application was originally used PHP's native MySQL...
| |
by: Mohanasundaram |
last post by:
Hi All,
We are working on porting a product written in C and C++ from 32 bit
to 64 bit. We need to maintain both 32 bit and 64 bit versions in the
future. We took the 32 bit source code and copiled it using a 64 bit
compiler and fixed all the compilation warnings. Compilation went
through fine but the product breaks in lots of places. We understood
that porting a 32 bit code to 64 bit platform is not just a matter of
compilation. We...
|
by: Tone |
last post by:
I've written a large suite of ANSI C routines for a virtual hand-held
device. A hardware manufacturer with whom we wish to do business
prefers C#.
My understanding of C# is somewhat limited. I see that it is closer to
Java and C++.
What I want to know is this: how difficult would it be to port my ANSI
C code to C#?
|
by: Bill |
last post by:
Does anyone know how to fix this problem? On form submittal I'm getting the
following error in Visual Studio 2005:
The state information is invalid for this page and might be corrupted.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.
Exception Details: System.Web.HttpException: The state...
|
by: Ian |
last post by:
I would like to hear from others who have considered and/or ported code from
traditional C++ to C++/CLI.
The class library I am considering porting to C++/CLI was written in
traditional C++ with STL and a touch of MFC. It represents the back end to
my applications and, among a number of other things, it performs all file
I/O and data management operations (i.e. no GUI). Why would I consider
porting it to C++/CLI when it works just...
|
by: subramanian100in |
last post by:
Is there any difference between porting and migrating.
Kindly explain
|
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...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |