Search code examples
network-programmingprotocolsendianness

Bit ordering in a byte, does it matter?


Let's say we have two machines on a network MA and MB,
MA considers little endian the order of the bits in a byte,
on the contrary MB considers big endian the order of the bits in a byte.

How do MA and MB agree on what "endianess" to use for the bits in a byte during the communication over the network ?

Is there a standard "network endianess" or what ?
Do socket programmers have to take any actions in ensuring a correct communication ?

For example HTTP is a text protocol, that means that machines send and receive bytes which represent characters,what if the encoding of the characters is different in the endianess of the bits ?


Solution

  • Yes, the hardware protocols specify the bit order of bytes on all network links. This is generally handled automatically by the NIC hardware.

    See, for example, this description of Ethernet frame format.

    Ethernet transmission is strange, in that the byte order is big-endian (leftmost byte is sent first), but bit order little-endian (rigthmost, or LSB (Least Significant Bit) of the byte is sent first).