Bit fields in python?

Justin Mayfield jmayfield at cradlepoint.com
Wed Sep 8 19:03:04 EDT 2010


Instead of rewriting your code you might consider wrapping it with the 
C-API.  I prefer this approach (over ctypes) for anything "low level".

http://docs.python.org/c-api/

On 09/06/2010 10:06 PM, Kwan Lai Cheng wrote:
> Hi,
> I'm trying to rewrite a c program in python & encountered several 
> problems. I have some data structures in my c program like below:
> typedef struct
> {
>     unsigned short size;
>     unsigned short reserved:8;
>     unsigned short var_a1:2;
>     unsigned short var_a2:2;
>     unsigned short var_a3:2;
>     unsigned short var_a4:2;
>     unsigned int var_a5;
> }structa;
> typedef struct
> {
>     unsigned short size;
>     unsigned char reserved:4;
>     unsigned char var_b1:1;
>     unsigned char var_b2:1;
>     unsigned char var_b3:1;
>     unsigned char var_b4:1;
>     structa var_structa;
> }structb;
> I tried to code the above in python but only got this far:
> class StructA(object):
>     def __init__(self, size=0)
>     self.size = size
> class StructB(object):
>     def __init__(self, size=0)
> Any equivalent for c data structures & bit fields in python? And how 
> do I define var_structa (in structb) in python?
> Regards,
> Kwan.
> !SIG:4c85bc4844361237431282! 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100908/f4e2c829/attachment-0001.html>


More information about the Python-list mailing list