Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to eliminate the bitmap difference in big endian and little endian?

Reply
Thread Tools

How to eliminate the bitmap difference in big endian and little endian?

 
 
Eric J.Hu
Guest
Posts: n/a
 
      08-29-2005
Hi,

Such as I have following bitmap structure and assigned value:
struct bm
{
char high2bits:2;
char mid4bits:4;
char low2bits:2;
};
bm.high2bits = 3;
bm.mid4bits = 0;
bm.low2bits = 0;

The char value is 0x03 in little endian mode, and the char value is 0xc0 in big endian mode. How to eliminate the bitmap difference?


Thanks,
Eric
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      08-29-2005
"Eric J.Hu" <> writes:
> Such as I have following bitmap structure and assigned value:
> struct bm
> {
> char high2bits:2;
> char mid4bits:4;
> char low2bits:2;
> };
> bm.high2bits = 3;
> bm.mid4bits = 0;
> bm.low2bits = 0;
>
> The char value is 0x03 in little endian mode, and the char value is 0xc0
> in big endian mode. How to eliminate the bitmap difference?


The only allowed types for bit-fields are int, signed int, unsigned
int (plain int may be either signed or unsigned in this context only)
and _Bool (C99 only). Your compiler may allow char bit-fields, but
it's non-portable. You probably want to use unsigned int.

Layout of bit-fields is implementation-specific. If you want
portability between little-endian and big-endian systems, don't use
bit-fields at all; use an unsigned integer type and manipulate the
bits yourself.

--
Keith Thompson (The_Other_Keith) kst- <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.
 
Reply With Quote
 
 
 
 
Malcolm
Guest
Posts: n/a
 
      08-29-2005

"Eric J.Hu" <> wrote

> Such as I have following bitmap structure and assigned value
> The char value is 0x03 in little endian mode, and the char value is 0xc0
> in big endian mode. How > to eliminate the bitmap difference?
>

You shouldn't normally need to care about the endianness of your host
machine.
You do need to care about the endianness of binary format files.
Never read or write a structure directly to a file that needs to be ported.
Write the functions write16le(int x, FILE *fp) to write a 16-bit
little-endian integer to a file, and so on. Write each field individually.
That way the code works whatever happens to the C compiler's internal
structure layout.


 
Reply With Quote
 
Alexei A. Frounze
Guest
Posts: n/a
 
      08-29-2005
Don't use bit fields, and in general, don't rely on any bit/byte layout. If
you have a short, int or long, use them as whole, don't access them
partially through pointers to chars. Rather, extract the appropriate bit
from its bit position by means of a bit mask, e.g. to set a bit in an int at
position n:
myint |= 1<<n;
to reset it:
myint &= ~(1<<n);
to check it:
if (myint & (1<<n)) op1(); // if bit's set
else op2(); // if bit's clear

And don't save any nontext data (anything other than array of characters) to
a file using fwrite(), likewise don't read with fread() anything other than
characters from a file. And you should be OK and free from the endianness.
You may develop functions to decompose short, int and long into a series of
chars using the methods similar to the above, something like this:

int i;
FILE *f = ...;
unsigned x = ...;
for (i=0; i<sizeof(x); i++)
{
unsigned char c = x & UCHAR_MAX;
fwrite (&c, 1, 1, f);
x >>= CHAR_BIT;
}

the above will save the entire integer x in LSB first way regardless of the
CPU's endianness.
It's a good question whether or not I should use 8 instead of CHAR_BIT... I
know some odd system (a DSP) where char is 16-bit long, there, I believe,
the bits 8 through 15 are ignored when char is saved. Actually, no, they
shouldn't be ignored, but then again, the file is still a sequence of 8-bit
chars (the DSP saves files to the PC through JTAG). I'm not sure of this
particular thing as I usually saved raw data in 16-bit pieces (shorts), not
any kind of text...

HTH,
Alex

"Eric J.Hu" <> wrote in message
news:deu5u9$...
Hi,

Such as I have following bitmap structure and assigned value:
struct bm
{
char high2bits:2;
char mid4bits:4;
char low2bits:2;
};
bm.high2bits = 3;
bm.mid4bits = 0;
bm.low2bits = 0;

The char value is 0x03 in little endian mode, and the char value is 0xc0 in
big endian mode. How to eliminate the bitmap difference?


Thanks,
Eric


 
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
Big-endian, little-endian and sizeof() in different systems Javier C++ 6 06-16-2007 12:43 PM
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



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57