Bitfield entity in gcc

The specificity of the bitfields is determined by the implementation. Is there a way to check, at compile time, through some macro or another compiler flag, what is gcc bitfield endianness?

In other words, given something like:

struct X {
    uint32_t a : 8;
    uint32_t b : 24;
};

Is there a way to find out at compile time if the afirst or last byte is in X?

+4
source share
1 answer

On Linux systems, you can check the macro __BYTE_ORDERto see if it is __LITTLE_ENDIANor __BIG_ENDIAN. Although this is not authoritative, in practice it should work.

, , struct iphdr netinet/ip.h, IP-. 4- , , :

struct iphdr
  {
#if __BYTE_ORDER == __LITTLE_ENDIAN
    unsigned int ihl:4;
    unsigned int version:4;
#elif __BYTE_ORDER == __BIG_ENDIAN
    unsigned int version:4;
    unsigned int ihl:4;
#else
# error "Please fix <bits/endian.h>"
#endif
    u_int8_t tos;
    u_int16_t tot_len;
    u_int16_t id;
    u_int16_t frag_off;
    u_int8_t ttl;
    u_int8_t protocol;
    u_int16_t check;
    u_int32_t saddr;
    u_int32_t daddr;
    /*The options start here. */
  };
+2

Source: https://habr.com/ru/post/1690151/


All Articles