Why bit endianness is an issue in bitfields?

后端 未结 7 1994
忘了有多久
忘了有多久 2020-11-22 03:12

Any portable code that uses bitfields seems to distinguish between little- and big-endian platforms. See the declaration of struct iphdr in linux kernel for an example of su

7条回答
  •  既然无缘
    2020-11-22 04:13

    As far as I understand, bitfields are purely compiler constructs

    And that's part of the problem. If the use of bit-fields was restricted to what the compiler 'owned', then how the compiler packed bits or ordered them would be of pretty much no concern to anyone.

    However, bit-fields are probably used far more often to model constructs that are external to the compiler's domain - hardware registers, the 'wire' protocol for communications, or file format layout. These thing have strict requirements of how bits have to be laid out, and using bit-fields to model them means that you have to rely on implementation-defined and - even worse - the unspecified behavior of how the compiler will layout the bit-field.

    In short, bit-fields are not specified well enough to make them useful for the situations they seem to be most commonly used for.

提交回复
热议问题