473,388 Members | 1,277 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

porting C code

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
Jul 18 '05 #1
8 2015
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
Jul 18 '05 #2
Lucas Raab <py*********@hotmail.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.
Jul 18 '05 #3
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
Jul 18 '05 #4
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
Jul 18 '05 #5
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.
Jul 18 '05 #6
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)|(rev32(n>>32)<<32)|(rev32(n)<<64)

print hex(rev96(0x102030405060708090a0b0c0L))

Jul 18 '05 #7
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
Jul 18 '05 #8
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.
Jul 18 '05 #9

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

Similar topics

2
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...
2
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...
7
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...
5
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
4
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,...
16
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...
6
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...
1
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....
4
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...
34
by: subramanian100in | last post by:
Is there any difference between porting and migrating. Kindly explain
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.