We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,650 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

structs+unions

//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;
};
2
Contributors
3
Replies
1 Week
Discussion Span
1 Year Ago
Last Updated
4
Views
guest3
Light Poster
29 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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?

L7Sqr
Practically a Posting Shark
867 posts since Feb 2011
Reputation Points: 253
Solved Threads: 155
Skill Endorsements: 7

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

guest3
Light Poster
29 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

L7Sqr
Practically a Posting Shark
867 posts since Feb 2011
Reputation Points: 253
Solved Threads: 155
Skill Endorsements: 7

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0758 seconds using 2.73MB