473,320 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Writing C readable bitfield structs?

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
6 2028
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
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
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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Grumfish | last post by:
In order to familiarize my self with Flash files and their bytecode I've started to make an assembler. My first problem is writing the bitfields the format uses often. It is a series of fields,...
9
by: Davide Bruzzone | last post by:
Greetings all... I need to create a number of bitfield structs whose contents are smaller than the size of an int. For example: typedef struct foo FOO; struct foo { unsigned char fieldOne:...
4
by: Ray | last post by:
When a single-bit bitfield that was formed from an enum is promoted/cast into an integer, does ANSI C say anything about whether that integer should be signed or unsigned? SGI IRIX cc thinks it is...
3
by: Andy Venikov | last post by:
Sometimes you want to use a bitfield to hold an enum value. In such cases you would only use as many bits as are needed to encode the full set of enum values. And it is a pain to recompute and...
4
by: | last post by:
Hi, How do we marshall a type like this from a C++/CLI class wrapper to an unmanaged method? typedef struct { UINT32 blah : 1; UINT32 blah2 : 1; UINT32 blah3: 1;
31
by: Zero | last post by:
Hi everybody! Is it possible to write a value into a bit field which initializes all bits? example: struct Field { unsigned int Bit1 : 1;
0
by: hortitude.eyeball | last post by:
I have an xml doc that has an element that is a bitfield. I am trying to write an xsl document that will be able to translate this to a readable form. Bit 1 = water Bit 2 = coffee Bit 3 =...
7
by: arne | last post by:
Hi all, cleaning up some elderly code, I stumbled across the following: /**************************************************/ struct { uint bf:8; char a1; char a2;
6
by: shaun roe | last post by:
For a bit of seasonal festive fun, I thought I'd try making a bitfield function, i.e. a function returning, for example, the value of bits 1 to 5 of a word as an integer, when the exact bits are...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.