This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Packed Structs with Bitfields

Hi Guys,

I'm programing my own TCP/Ip Stack. Therefore I need structs with bitfields.

For eg:

typedef struct  __attribute__ ((packed))
{
        uint16_t srcPort;
        uint16_t dstPort;
        uint32_t seq;
        uint32_t ack;
        unsigned headerlen      : 4;
        unsigned res            : 6;
        unsigned flags          : 6;
        uint16_t hlenFlags;
        uint16_t windowSize;
        uint16_t checksum;
        uint16_t urgentPtr;
}
TCP_Header

After the bitfield variables the values aren't right anymore. Can you tell me why is this so?

Thanks for your help
best regards
Robert

Parents
  • To add to what Per said.
    Do not rely on bit fields which are ALWAYS compiler specific to work with hardware which is ALWAYS hardware specific. You will never be able to upgrade your compiler or use a different one if you do this. Read words and use bit operations as needed but use of bit fields is precarious and fraught with much configuration twiddling.

    This is a feature that can't be relied on with hardware. It gets REALLY entertaining with doing floating point operations and dealing with single half and double precision data fields. That's all software and using bit fields won't work with them as 'expected'.

    Stephen

Reply
  • To add to what Per said.
    Do not rely on bit fields which are ALWAYS compiler specific to work with hardware which is ALWAYS hardware specific. You will never be able to upgrade your compiler or use a different one if you do this. Read words and use bit operations as needed but use of bit fields is precarious and fraught with much configuration twiddling.

    This is a feature that can't be relied on with hardware. It gets REALLY entertaining with doing floating point operations and dealing with single half and double precision data fields. That's all software and using bit fields won't work with them as 'expected'.

    Stephen

Children
No data