"jlara" <jl***@wowway.com> writes:
Having been stung by the same problem twice, I would like to
automatically check if the bitfields are regarded properly by the
compiler. For example, if I define the following structure:
typedef struct
{
unsigned int : 4;
unsigned int tffca : 1;
unsigned int tsfrz : 1;
unsigned int tswai : 1;
unsigned int ten : 1;
}tTSCR1;
Is there a way to check if the compiler considers bitfield ten to be
the most significant bit?
If there is, I would like to have the compiler give an error message (
#error ) and abort.
I don't believe there's any way to check endianness at compilation
time. You can check it at run time. For example, declare a union of
the above struct and an unsigned char, check that sizeof(tTSCR1)
yields 1 and that CHAR_BIT==8, and for each named bit field, set the
unsigned char member to 0, set the bit field to 1, and check that the
unsigned char takes on the expected value. If any of these tests
fails, abort the program.
(Incidentally, "endianness" usually refers to the ordering of bytes
within a word; you're looking at bit order within a byte.)
If you don't want to perform this test every time the program runs,
you can incorporate it into your build procedure. Before building
your application, build and run a test program that performs the
appropriate tests; abort the build if the test program fails.
(Methods for doing this are off-topic; depending on your system, the
words "configure" and "Makefile" may provide a clue.)
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.