By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,986 Members | 1,583 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,986 IT Pros & Developers. It's quick & easy.

Writing C readable bitfield structs?

P: n/a
How would I go about writing a bitfield that can be read by my C app? I
want to pack a few bools into one int.

I know an extended module exists (npstruct) which helps you do this but
I want to do it manually or using one of the standard modules.

Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ph*****@yahoo.com wrote:
How would I go about writing a bitfield that can be read by my C app? I
want to pack a few bools into one int.

I know an extended module exists (npstruct) which helps you do this but
I want to do it manually or using one of the standard modules.

struct.pack is in the Standard Library and allows you to do what
you want. You may find that you must also do some "bit twiddling"
using << shift functions and | bit or function if you want to pack
tighter than on 4 bit boundaries.

Larry Bates
Jul 18 '05 #2

P: n/a
there is a bitfiled mainpulator class inthe Cookbook, but I don't
understand his explanation, and the example given doesn't really show
off the features of the class.

I too need bit-level manipulation, and will probably have to write my
own class to do it.

Jul 18 '05 #3

P: n/a
In article <11**********************@l41g2000cwc.googlegroups .com>,
Cappy2112 <ca*******@gmail.com> wrote:
there is a bitfiled mainpulator class inthe Cookbook, but I don't
understand his explanation, and the example given doesn't really show
off the features of the class.


I assume you're talking about the struct module? If you give an
example of the C struct you're trying to read/write, I could come up
with some sample code to do it.
Jul 18 '05 #4

P: n/a

Roy Smith wrote:
In article <11**********************@l41g2000cwc.googlegroups .com>,
Cappy2112 <ca*******@gmail.com> wrote:
there is a bitfiled mainpulator class inthe Cookbook, but I don't
understand his explanation, and the example given doesn't really showoff the features of the class.


I assume you're talking about the struct module? If you give an
example of the C struct you're trying to read/write, I could come up
with some sample code to do it.


struct S {
unsigned int a : 1;
unsigned int b : 1;
unsigned int c : 1;
unsigned int d : 1;
};

fread(from file (file written by Python app) into an instance of struct
S)
then I want it to be used as follows:
if (instance.a) f();
if (instance.b) g();
struct S comes out to 4 on my arch. I do not want to use a new int for
every member of struct S.

Jul 18 '05 #5

P: n/a
Anyone have any idea?

ph*****@yahoo.com wrote:
Roy Smith wrote:
In article <11**********************@l41g2000cwc.googlegroups .com>,
Cappy2112 <ca*******@gmail.com> wrote:
there is a bitfiled mainpulator class inthe Cookbook, but I don't
understand his explanation, and the example given doesn't really showoff the features of the class.
I assume you're talking about the struct module? If you give an
example of the C struct you're trying to read/write, I could come up with some sample code to do it.


struct S {
unsigned int a : 1;
unsigned int b : 1;
unsigned int c : 1;
unsigned int d : 1;
};

fread(from file (file written by Python app) into an instance of

struct S)
then I want it to be used as follows:
if (instance.a) f();
if (instance.b) g();
struct S comes out to 4 on my arch. I do not want to use a new int for every member of struct S.


Jul 18 '05 #6

P: n/a

ph*****@yahoo.com TOP-POSTED:
Anyone have any idea?


1. Larry Bates has already told you.

2. I note that you say "I do not want to use a new int for every member
of struct S.", *not* "I am forced to pack bools into an int, 1 bit per
bool, because I have no control over the file format". Quite a
difference.

3. One could ask: How you would do it in your C app, if C didn't have
bitfields in structs?

Never mind, I'll give a bit more detail:

In your Python script:

!APOS = 0; BPOS = 1; CPOS = 2
!for each record: # pseudocode
! outint = 0
! if is_a: outint |= (1 << APOS)
! if is_b: outint |= (1 << BPOS)
! if is_c: outint |= (1 << CPOS)
! etc

Note that AFAIK, C makes no guarantee about the order of bitfields in
structs, nor whether they start at the big end or the little end of the
int that contains them; so you may need to change the APOS etc numbers.

If you do have control over your C app, you could use the struct module
to pack the bools one per byte, and remove any concerns about the
idiosyncracies of C compilers and the endianness of the source and
target architectures. You could even make the file not only eyeballable
but robust by representing the values as "T" and "F" instead of "\1"
and "\0" (recalling that by ancient tradition C programs are likely to
stuff up mightily when presented with "\0" in data).

Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.