On Fri, 8 Aug 2008 22:43:53 0700 (PDT), Ramesh <(EMail Removed)>
wrote:
>Hi
>
>I have a structure as below on big endian based system
>
>typedef struct
>{
>
>unsigned long LedA:5;
unsigned long is not one of the standard types allowed for bitfields.
Does your implementation actually document it as an extension as
required by 6.7.2.14.
>unsigned long LedB:4;
You need to look at 6.7.2.110 to see the possible implementations
when two adjacent bitfields won't fit in a byte.. Which one does
your implementation document?
The same section talks about the order in which bit fields are
defined. Which one does your implementation document?
>unsigned long LedC:8;
>unsigned long LedD:4;
>unsigned long LedE:5;
>unsigned long LedF:6;
>
>}regs_t;
>
>#define get_LedStatus(x) ((regs_t *)(&x))>LedA
>
>main() {
>
>unsigned long LedReg = 0x80000000;
>printf("%X", get_LedStatus(LedReg))
%X expects its argument to be an unsigned int. You invoke undefined
behavior by passing an unsigned long.
>
>}
>
>On a big endian based system  I get a value of 0x1 as output.
You need to show the actual output. %X does not produce a leading 0x.
Are you sure your code as seven zeros after the eight in the
initialization value. If so, I would have expected output of 10.
Your output makes sense if there are only 6 zeros.
Is a long actually 32 bits on your system? Shouldn't your
initialization value have UL affixed?
>
>For the little endian based system, I define the structure as below:
>
>typedef struct
>{
>
>unsigned long LedF:6;
>unsigned long LedE:5;
>unsigned long LedD:4;
>unsigned long LedC:8;
>unsigned long LedB:4;
>unsigned long LedA:5;
>
>}regs_t;
>
>I get a value as 0x10, so instead of getting a bit pattern like 0
>0001
>what I get is 1 0000
>
>While reversing the bits solves the problem  Any insights as to what
>could be the best solution if I have several such structures to be
>ported from big endian to little endian system?
If you define the structure with an unsigned integer of the correct
size, you can use the <<, >>, and && operators to extract the desired
fields. Since they work on values and not representations, you may be
able to ignore the problem of endianness altogether.

Remove del for email
