Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Setting Bitfields on big endian and little endian

Reply
Thread Tools

Setting Bitfields on big endian and little endian

 
 
Ramesh
Guest
Posts: n/a
 
      08-09-2008
Hi

I have a structure as below on big endian based system

typedef struct
{

unsigned long LedA:5;
unsigned long LedB:4;
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))

}

On a big endian based system - I get a value of 0x1 as output.

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?

BTW the target is Linux 2.6 on x86 & gcc 4.12.2xxxx

Thanks
/R
 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      08-09-2008
On Fri, 8 Aug 2008 22:43:53 -0700 (PDT), Ramesh <(E-Mail 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 bit-fields.
Does your implementation actually document it as an extension as
required by 6.7.2.1-4.

>unsigned long LedB:4;


You need to look at 6.7.2.1-10 to see the possible implementations
when two adjacent bit-fields 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
little endian , big endian , big headache manishster@gmail.com C Programming 5 08-31-2006 12:28 AM
Big Endian and Little Endian bhatia C++ 2 07-07-2006 06:48 PM
What's the memory layout of bit field struct in little-endian and big-endian platform? aling C++ 8 10-19-2005 04:50 PM
How to eliminate the bitmap difference in little endian and big endian? Eric J.Hu C++ 7 09-07-2005 10:27 AM
How to eliminate the bitmap difference in big endian and little endian? Eric J.Hu C Programming 3 08-29-2005 09:22 PM



Advertisments