Writing C readable bitfield structs?

P

phark52

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.
 
L

Larry Bates

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
 
C

Cappy2112

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.
 
R

Roy Smith

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.
 
P

phark52

Roy said:
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.
 
P

phark52

Anyone have any idea?

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.
 
J

John Machin

(e-mail address removed) 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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top