Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   How to eliminate the bitmap difference in big endian and little endian? (http://www.velocityreviews.com/forums/t439221-how-to-eliminate-the-bitmap-difference-in-big-endian-and-little-endian.html)

Eric J.Hu 08-29-2005 05:18 AM

How to eliminate the bitmap difference in big endian and little endian?
 
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

Keith Thompson 08-29-2005 06:39 AM

Re: How to eliminate the bitmap difference in big endian and littleendian?
 
"Eric J.Hu" <ehu@lucent.com> 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-u@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.

Malcolm 08-29-2005 07:03 PM

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

"Eric J.Hu" <ehu@lucent.com> 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.



Alexei A. Frounze 08-29-2005 09:22 PM

Re: How to eliminate the bitmap difference in big endian and little endian?
 
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" <ehu@lucent.com> wrote in message
news:deu5u9$qp7@netnews.net.lucent.com...
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




All times are GMT. The time now is 02:19 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


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