Jeremy
Jeremy

Reputation: 103

Union/Structure datatype and bit field referencing speed

Using the following union typedef, is there a difference in speed between accessing MyPacket.Byte.LB compared with MyPacket.Field.LB?

typedef union       // create a union to assemble bytes into ints
{
int Packet;     //  contains an int
struct
    {
    char HB;
    char LB;    
    }Byte;
struct
    {
    unsigned    field1:4;
    unsigned    field2:2;
    unsigned    field3:1;
    unsigned    field4:1;
    unsigned    LB:8;
    }Field;
} packetunion;

packetunion MyPacket;

Upvotes: 0

Views: 810

Answers (1)

John Bode
John Bode

Reputation: 123558

The only way to know for sure is to code up both versions and profile them on the target system, for multiple compiler optimization settings. I'd be genuinely surprised if you saw a measurable difference.

Upvotes: 1

Related Questions