Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__LITTLE_ENDIAN_BITFIELD and __BIG_ENDIAN_BITFIELD? [duplicate]

Tags:

c

linux-kernel

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
......
};
like image 681
wartalker Avatar asked Jan 25 '26 19:01

wartalker


1 Answers

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.

like image 63
chux - Reinstate Monica Avatar answered Jan 28 '26 10:01

chux - Reinstate Monica