473,324 Members | 2,473 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,324 software developers and data experts.

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

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
Nov 14 '05 #1
1 4633
mrhicks wrote:
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;

--
Best regards,
Andrey Tarasevich

Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Jeff Melvaine | last post by:
I note that I can write expressions like "1 << 100" and the result is stored as a long integer, which means it is stored as an integer of arbitrary length. I may need to use a large number of...
3
by: Mark Miller | last post by:
I have a char array and when I write it to a file using BinaryWriter the position of the pointer is the size of the array + 1. For example: writing char leaves the pointer at position 26 after...
8
by: Sowen | last post by:
Hi, all I am wondering how to write bits by using ofstream? I have finished a huffman tree, but how can I write the bits to the file in order to gain compression? for example, 'A' returns a...
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
4
by: Dave Rahardja | last post by:
I have the following program that uses an array of chars to simulate a bit set: --------- // An out-of-bounds exception class BoundsException {}; template <int bits = 1> class Bitset
22
by: Vijay | last post by:
With the option strict On set..... Dim fs As FileStream = File.OpenRead(strFile) With fs Dim buffer(fs.Length) As Byte ' <--- Option Strict On disallows implicit conversions from 'Long' to...
39
by: Mark Odell | last post by:
I've always declared variables used as indexes into arrays to be of type 'size_t'. I have had it brought to my attention, recently, that size_t is used to indicate "a count of bytes" and that using...
10
by: pcnerd | last post by:
I'm a VB.NET newbie. I've created a program that plots pixels at random on the form. I have a 19" LCD monitor with a resolution set to 1280 by 1024. If you do the math, that means that there are...
4
by: Ravi4raj | last post by:
Hi all, Here i have to know is thr any way to sort the array according to thr Bit Reverse.. for Example: If we take the Array size of 256,then the Sorting Should be like below. a =...
14
by: KK | last post by:
Dear All I have a small problem with using as operator on value type array. Here is an example what I am trying to do. using System; using System.Collections.Generic; using System.Text;
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.