Having a piece of code like the following:
typedef union
{
struct bits
{
uint32_t bit0 : 1;
uint32_t bit1 : 1;
uint32_t bit2 : 1;
...
...
uint32_t bit14 : 1;
}
uint32_t value;
} MyUnion;
It's important to prevent the compiler from reordering the bitfields since, otherwise, the value of value
will change depending on the platform and, possibly, even the size of the compiled binary.
Is there any way to tell the compiler "do not re-order this fields"?
Would declaring variables of type MyUnion
as volatile
achieve this?
If the union
is packed
and aligned(4)
, is it possible to assure that the bitfields won't be reordered, if you are 100% sure that you will be always working with 32 bits platforms, with the same endianness?
EDIT
Can the above be done for only one specific compiler? That is, if being compatible across different compilers is not necessary.