Using an array Bit-Field in struct, how to best approach...

M

mrhicks

Hello all,

I am trying to keep my coding easy for everyone to use. I have some
ARINC data that need to go out which 12 Bytes long. The first byte is
the command word, the next 10 bytes represent 40 devices in the system
with four command that can be issue per device. So within the each
byte of the 10 bytes a command comprises 4 device. The last byte is a
send flag.

I first tried creating a struct like the following...

struct CMD_DEVICE {
unsigned char cmd;
unsigned char device[40]:2;
unsigned char flag;
};

As I found out, this is illegal in C and can't be used. Okay, the
next try was to do the following..

struct DEVICE {
unsigned char device:2;
};

struct CMD_DEVICE {
unsigned char cmd;
struct CMD_DEVICE device[40];
unsigned char flag;
};


Which compiles fine, but the memory footprint is off and is treating
each 'device' struct as a byte and not a bit-field. So I can't use
that. I can get it to work like the following

struct CMD_DEVICE {
unsigned char cmd;
unsigned char dev1:2;
unsigned char dev1:2;
...
unsigned char dev39:2;
unsigned char dev40:2;
unsigned char flag;
};

This is fine and work okay, as I can tell so far, but it just looks
nasty and ugly as a declaration. The problem is that I have similar
ARINC data that comes down and it 3 or 4 bits. I would like to avoid
creating a huge structure declaration for each type. Is there a better
way of doing this? Any help is greatly appreciated.

Mark
 
A

Andrey Tarasevich

mrhicks said:
I first tried creating a struct like the following...

struct CMD_DEVICE {
unsigned char cmd;
unsigned char device[40]:2;
unsigned char flag;
};

As I found out, this is illegal in C and can't be used. Okay, the
next try was to do the following..

struct DEVICE {
unsigned char device:2;
};

struct CMD_DEVICE {
unsigned char cmd;
struct CMD_DEVICE device[40];
unsigned char flag;
};


Which compiles fine,

This will not compile. You probably meant to declare the data field as
'struct DEVICE device[40]'.
but the memory footprint is off and is treating
each 'device' struct as a byte and not a bit-field.

Yes, that's how it is supposed to work.
So I can't use
that. I can get it to work like the following

struct CMD_DEVICE {
unsigned char cmd;
unsigned char dev1:2;
unsigned char dev1:2;
...
unsigned char dev39:2;
unsigned char dev40:2;
unsigned char flag;
};

This is fine and work okay, as I can tell so far, but it just looks
nasty and ugly as a declaration.

But in this case you loose ability to address a device enetry by its
run-time index. Do you really care to keep this ability?
The problem is that I have similar
ARINC data that comes down and it 3 or 4 bits. I would like to avoid
creating a huge structure declaration for each type. Is there a better
way of doing this? Any help is greatly appreciated.

Another approach is to simulate bit-array with bitwise operators.
Declare your array as 'unsigned char device[10]' (40 * 2 / 8 = 10,
assuming that CHAR_BIT is 8 on your system). Now accessing a two-bit
entry for device 'i_device' can be implemented as follows

unsigned i_entry = i_device / 4;
unsigned n_shift = (i_device % 4) * 2;
unsigned char n_data = (cmd.device[i_entry] >> n_shift) & 0x03;
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top