porting C code

Steven Bethard steven.bethard at gmail.com
Thu Jan 13 19:59:37 EST 2005


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



More information about the Python-list mailing list