Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Bit fields in python?

11 views
Skip to first unread message

Stefan Behnel

unread,
Sep 7, 2010, 2:55:54 AM9/7/10
to pytho...@python.org
Kwan Lai Cheng, 07.09.2010 06:06:
> 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?

If you can tell us what these structs are being used for in the original C
code, we might be able to point you to a suitable way to implement the same
thing efficiently in Python.

Stefan

Kwan Lai Cheng

unread,
Sep 7, 2010, 4:39:48 AM9/7/10
to pytho...@python.org
----- Original Message -----
From: Stefan Behnel
To: <pytho...@python.org>
Sent: Tuesday, September 07, 2010 2:55 PM
Subject: Re: Bit fields in python?


>
> If you can tell us what these structs are being used for in the original C
> code, we might be able to point you to a suitable way to implement the
> same
> thing efficiently in Python.
>
> Stefan
>

> --
> http://mail.python.org/mailman/listinfo/python-list
>

It's a test program for testing the DSP (Digital Signal Processor).
Basically what it does is that it sends commands to the DSP via a driver and
waits for a response from the DSP. The structures represent the data that I
might receive from the DSP. For example if I send command A, DSP will send
data in the format of structa to me, so I will cast the data I received to
structa, and print the struct field values on screen. Each field in the
structures represent some status or info about the DSP, different bits in an
unsigned char or unsigned short field might represent different DSP status.

I just found out that there's a ctypes module
(http://docs.python.org/library/ctypes.html) in python that might solve my
problem. I understand that it is already included in Python 2.5 & I'm using
Python 2.6.2. However when I tried "from ctypes import *", I got "No module
named _ctypes". What am I missing here?


Terry Reedy

unread,
Sep 7, 2010, 2:28:38 PM9/7/10
to pytho...@python.org
On 9/7/2010 12:06 AM, 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;

In Python, we cannot directly name bitfields within an int. However, we can
*read, set, and flip bits with the bit operators and bit masks
*define a dict that maps bitfield names to bit indexes
*define named functions that use the above.
*wrap or subclass int with a class that has attributes that map to a
bitfield.

I am pretty sure there is public code that does all of the above.
Searching pypi.python.org for 'bitfield', I found

BitDecoder 0.5.1

Decode bit-fields to human readable description

This program (and Python module) will decode a value as per a bitfield
definition. This is very useful for hardware registers that have
meanings to parts of the bits in seperate.

Google for more.

--
Terry Jan Reedy

John Nagle

unread,
Sep 7, 2010, 3:13:41 PM9/7/10
to
On 9/6/2010 11:55 PM, Stefan Behnel wrote:
> Kwan Lai Cheng, 07.09.2010 06:06:
>> I'm trying to rewrite a c program in python& encountered several
>> problems. I have some data structures in my c program like below:
>>
def __init__(self, size=0)
>>
>> Any equivalent for c data structures& bit fields in python?
>
> If you can tell us what these structs are being used for in the original
> C code, we might be able to point you to a suitable way to implement the
> same thing efficiently in Python.

Python has the "struct" module for formatting binary data.

http://docs.python.org/library/struct.html

But it doesn't handle bit fields. Its lowest resolution is
one byte.

John Nagle

geremy condra

unread,
Sep 8, 2010, 6:55:21 AM9/8/10
to Eli Bendersky, pytho...@python.org
On Tue, Sep 7, 2010 at 9:53 PM, Eli Bendersky <eli...@gmail.com> wrote:
>>
>> 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?
>>
>
> Bitfields are most commonly used for extreme space optimization - i.e.
> shoving several variables and flags with carefully limited ranges into a
> single work. In Python you rarely work this way (where such an optimization
> is warranted, Python isn't the best tool for the job). However, as in your
> use case, it is sometimes needed in Python in order to communicate with
> other devices over the network or some other link.
>
> In my work with Python and embedded devices I've found the construct library
> (http://construct.wikispaces.com/) very useful. It allows to you very easily
> define complex formats for frames/messages on the bit and byte level. The
> idea is to use construct to encode and decode messages being sent to an
> embedded device. It works great.
>
> If you have further questions about this approach, feel free to ask.
>
> Eli

That's really an excellent find. Thanks for bringing it up.

Geremy Condra

0 new messages