The endianness of bitfields is implementation defined. Is there a way to check, at compile time, whether via some macro or other compiler flag, what gcc's bitfield endianness actually is?
In other words, given something like:
struct X {
uint32_t a : 8;
uint32_t b : 24;
};
Is there a way for me to know at compile time whether or not a
is the first or last byte in X
?
htonl
and back withntohl
? – Gotchermove.l
instruction is going to read/write the value to memory and what will((char*)(&x))[0]
return once the move instruction is done. C has no control over it, and no sane compiler would add special code to deal with this. That's why you have functions likehton
– Gotchermove.l
instruction. There are times when you are running on, say, an Intel processor, but you want to emulate the environment of another processor, perhaps because you are developing new software for the other processor but do not have hardware yet. The C implementation must provide the endianness it will ultimately have on the future hardware, but it has to run on the endianness of the current hardware. – Orndorff