1.11M Members

structs+unions

 
0
 

//use nib in Nibbler to set nib1, nib2, nib3. How the Nibbler and ABC get more efficient to send the Nbytes in ABC?.

union ABC {
    unsigned char Nbytes[4];
    struct 
        {
                unsigned short nib1    : 4; 
                unsigned char  NA1     : 2;
                unsigned short nib2    : 4;
                unsigned short nib3    : 4;
                unsigned char  NA2     : 2;
        } bits;
     
};

union Nibbler {
    struct {
        unsigned char Bits : 4; 
        } nib;
    unsigned char N;
};
 
0
 

I dont understand that question. If you need to send 4 bytes of information then you need 4 bytes of storage. I'm not sure how you expect to get more efficient than that.

Could you please explain a little better what it is you are trying to do?

 
0
 

Have a structure that has fields with different number of bits, i.e., 13, 6 , 4 and totals to 19 bytes when packed together. How to pack the structure and union this structure with array of BYTES so that any change to any bit will be reflected accuratelty into the corresponding BYTE. I use V.S.2005. any code that can do this?.

 
0
 

A union allows you to have two (or more) members share the same memory. In that manner you will always see a change in the byte array if you modify the bits of the structure. The problem you may run into is packing/padding. For instance given the following structure

struct foo {
   unsigned a : 3;
};

it may be the case that sizeof (struct foo) is actually 4 (bytes). That is certainly less efficient that say storing the bits in a char which is 1 byte.
It really boils down to what you want to represent. If you want to store the value of a bitfield in a char variable then you set up a structure with bitfields and padding and use a union with the appropriately sized character buffer. If you want to minimize the space you use when storing your bits then you choose the character array and manage the bit manipulations yourself.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: