I've been reading about the new additions to the C standard, and I've come across macros for determining the compile-time endianness.
However the standard still states that
The order of allocation of bit-fields within a unit (high-order to low-order or low-order to high-order) is implementation-defined.
Does that mean the layout of a bit-field can be whatever regardless of the value of __STDC_ENDIAN_NATIVE__
?
The bit order didn't depend on endianess in the first place. But rather if the first bit in the struct is considered MSB or LSB. For example, some implementation-specific bit-fields might just be 1 byte large.
Common sense says that the first bit ought to be MSB in my opinion, because that's how everyone but IBM enumerates bits, from highest to lowest (7 to 0). But apparently most implementations have it as LSB. You can try this example:
#include <stdio.h>
typedef struct
{
unsigned bit:1;
} bit_t;
int main (void)
{
bit_t bit = { .bit=1 };
unsigned char* byte = (unsigned char*)&bit;
if(*byte & 0x01)
{
puts("bit = LSB");
}
else if(*byte & 0x80)
{
puts("bit = MSB");
}
else
{
puts("Completely broken compiler.")
}
}
I tried it out for various targets on Compiler Explorer:
bit = LSB:
bit = MSB: