__LITTLE_ENDIAN_BITFIELD and __BIG_ENDIAN_BITFIELD? [duplicate]

房东的猫 提交于 2021-01-27 18:20:25

问题


I want to know what compiler of kernel will do with different endian bitfield:

struct iphdr {
#if defined(__LITTLE_ENDIAN_BITFIELD)
__u8
    ihl:4,
    version:4;
#elif defined (__BIG_ENDIAN_BITFIELD)
__u8
    version:4,
    ihl:4;
#else
    #error "Please fix <asm/byteorder.h>"
#endif
......
};

回答1:


The structure iphdr takes up 1 byte. In a little endian machine, the first field ihl occupies bits 0,1,2,3 and the 2nd field version occupies bits 4,5,6,7. ihl is listed first and gets the least significant bits. In the seconds case, version, being listed first gets the first bits. Since this is big endian, the first bits are bits 7,6,5,4. ihl get the next four bits, 3,2,1,0.

With the #if condition, regardless is one compiles in a big or little endian machine, the bits are in the same bit offset location within a byte.

With these field in the same bit localization, various masking and bit shift operations give the same result.


Interestingly the phrase little endian and big endian originate for the the story Gulliver's Travels. There, folks argue a senseless war about which end of a hard-boiled egg to crack first. Thus big endian vs. little endian. An apt name for a senseless debate about which is more correct. (I'm a little endian-er.)


Further, the same story introduces another computer term: yahoo.



来源:https://stackoverflow.com/questions/18070977/little-endian-bitfield-and-big-endian-bitfield

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!