I know that the byte order things are relevant when we talk about network data transmission, i.e. network byte order (big-endian) is not the native thing for our operation systems, so bytes should be converted (swapped). E.g. if we have uint32_t
with bytes 0x41
0x42
0x43
0x44
in memory in little-endian (OS-native) representation, for sending it through the network we must swap it to 0x44
0x43
0x42
0x41
.
But what is the problem with bits and bitfields?? Why bits inside one byte (uint8_t) can / need to be swapped while network communication?
Example from DPDK:
__extension__
struct rte_gtp_hdr {
union {
uint8_t gtp_hdr_info; /**< GTP header info */
struct {
#if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
uint8_t pn:1; /**< N-PDU Number present bit */
uint8_t s:1; /**< Sequence Number Present bit */
uint8_t e:1; /**< Extension Present bit */
uint8_t res1:1; /**< Reserved */
uint8_t pt:1; /**< Protocol Type bit */
uint8_t ver:3; /**< Version Number */
#elif RTE_BYTE_ORDER == RTE_BIG_ENDIAN
uint8_t ver:3; /**< Version Number */
uint8_t pt:1; /**< Protocol Type bit */
uint8_t res1:1; /**< Reserved */
uint8_t e:1; /**< Extension Present bit */
uint8_t s:1; /**< Sequence Number Present bit */
uint8_t pn:1; /**< N-PDU Number present bit */
#endif
};
};
uint8_t msg_type; /**< GTP message type */
rte_be16_t plen; /**< Total payload length */
rte_be32_t teid; /**< Tunnel endpoint ID */
} __rte_packed;
Why in this specific example bits order in bitfield depends on RTE_BYTE_ORDER
(keyword byte)? Bit order depends on byte order? O_o
One more example in Linux kernel.
Sorry if obvious..