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.