473,324 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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
Nov 15 '05 #1
3 4379
"Eric J.Hu" <eh*@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) ks***@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.
Nov 15 '05 #2

"Eric J.Hu" <eh*@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.
Nov 15 '05 #3
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" <eh*@lucent.com> wrote in message
news:de********@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
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Joe C | last post by:
I have some code that performs bitwise operations on files. I'm trying to make the code portable on different endian systems. This is not work/school related...just trying to learn/understand. ...
7
by: Eric J.Hu | last post by:
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;
8
by: Perception | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
2
by: bhatia | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
0
by: Craig | last post by:
Hi there, This could be a curly question. When I created the x11 bitmap image using the im.tobitmap() function I found out later that the display information in the array is big endian (I...
33
by: raghu | last post by:
Is it possible to know whether a system is little endian or big endian by writing a C program? If so, can anyone please give me the idea to approach... Thanks a ton. Regards, Raghu
8
by: ma740988 | last post by:
Data stored on a storage device is byte swapped. The data is big endian and my PC is little. At issue: There's a composite type ( a header ) at the front of the files that I'm trying to read in....
11
by: somenath | last post by:
Hi All , Can any body explain me be the output of the following program #include <stdio.h> int main(void) { int a={1,2,3,4}; char *p =(char *)&a; p++;
23
by: Niranjan | last post by:
I have this program : void main() { int i=1; if((*(char*)&i)==1) printf("The machine is little endian."); else printf("The machine is big endian."); }
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.