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

Discussion in 'C Programming' started by Eric J.Hu, Aug 29, 2005.

  1. Eric J.Hu

    Eric J.Hu Guest

    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
    Eric J.Hu, Aug 29, 2005
    #1
    1. Advertising

  2. Re: How to eliminate the bitmap difference in big endian and littleendian?

    "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) <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.
    Keith Thompson, Aug 29, 2005
    #2
    1. Advertising

  3. Eric J.Hu

    Malcolm Guest

    "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.
    Malcolm, Aug 29, 2005
    #3
  4. 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
    Alexei A. Frounze, Aug 29, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    333
    Stephen Sprunk
    Aug 31, 2006
  2. Eric J.Hu
    Replies:
    7
    Views:
    518
    Jim Langston
    Sep 7, 2005
  3. aling
    Replies:
    8
    Views:
    941
    Pete Becker
    Oct 19, 2005
  4. bhatia

    Big Endian and Little Endian

    bhatia, Jul 7, 2006, in forum: C++
    Replies:
    2
    Views:
    470
    Robbie Hatley
    Jul 7, 2006
  5. Javier
    Replies:
    6
    Views:
    558
    Gennaro Prota
    Jun 16, 2007
Loading...

Share This Page