Why bit endianness is an issue in bitfields?

后端 未结 7 2011
忘了有多久
忘了有多久 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 03:53

    ISO/IEC 9899: 6.7.2.1 / 10

    An implementation may allocate any addressable storage unit large enough to hold a bit-field. If enough space remains, a bit-field that immediately follows another bit-field in a structure shall be packed into adjacent bits of the same unit. If insufficient space remains, whether a bit-field that does not fit is put into the next unit or overlaps adjacent units is implementation-defined. The order of allocation of bit-fields within a unit (high-order to low-order or low-order to high-order) is implementation-defined. The alignment of the addressable storage unit is unspecified.

    It is safer to use bit shift operations instead of making any assumptions on bit field ordering or alignment when trying to write portable code, regardless of system endianness or bitness.

    Also see EXP11-C. Do not apply operators expecting one type to data of an incompatible type.

提交回复
热议问题