How to change bit-field allocation

James E Wilson wilson@specifixinc.com
Mon Sep 6 21:43:00 GMT 2004


Min ZOU wrote:
> As I know, GCC allocates bit-fields inside a structure from the lowest
> addressed byte, i.e. LSB, for little endian. However, I want to reverse
> the ordering for the bit-fields allocation, i.e. always from MSB.

You can't change the allocation order for bit-fields without changing 
the endianness of the processor.  A few moments thought should be enough 
to see the problem.  The normal rule is that a bitfield goes in the 
first avaiable bit.  You want it to go in the last available bit 
instead.  But the last bit of what?  If you want the last bit of the 
structure, then that is impossible to compute at this point because we 
don't know the size of the structure yet.  If you put it in the last bit 
of the current byte, then you run into problems if you have more than 8 
bits of bitfields.  If you put it in the last bit of the current word, 
then this probably works, but will add more padding then is absolutely 
necessary, and hence does not give exactly the same result as you would 
get for the other endianness.

What ever you are trying to do is probably wrong.  You probably should 
say what problem you are trying to solve, rather than asking how to 
implement particular solutions for it.
-- 
Jim Wilson, GNU Tools Support, http://www.SpecifixInc.com



More information about the Gcc mailing list