Endianness and bitfields of size 1

前端 未结 1 1960
一个人的身影
一个人的身影 2021-01-28 01:00

I thought that endianness is not supposed to affect structs of size at most 1 byte. But here\'s the code on my little endian machine:

#include 
#         


        
相关标签:
1条回答
  • 2021-01-28 01:27

    So let me gather all the info from comments.

    It seems that the order of bit fields is not specified in the standard and thus is implementation dependent. The relevant part of the standard is (thanks to @molbdnilo):

    §9.6: "Bit-fields are assigned right-to-left on some machines, left-to-right on others."

    Also let me note other comments. It seems that the order of bit fields is simply reversed in memory. This seems to be consistent with the order of normal fields as well and is probably consistent with endianness (I would appreciate if someone can check it on a big-endian machine).

    I guess that's what "right-to-left on some machines" and "left-to-right on others" means. However this is my interpretation and as stated earlier we should not depend on it.

    0 讨论(0)
提交回复
热议问题