473,379 Members | 1,170 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,379 software developers and data experts.

endian

Is there a standard conforming way to check for the endian of
the current machine? Possibly at compile time?

I am thinking of something like:

union endian
{
char c[2];
unsigned int i;
};

endian e; // can it be initialized directly?
e.i=1;

bool BigEndian()
{
return (e.c[0] == 0);
}

But this is not at compile time.
Further:
Is it guaranteed:
that an int has at least two bytes?
that the bit pattern for zero and one are the same
for char and int?

thanks,
marc

Jul 19 '05 #1
2 4629
On Fri, 11 Jul 2003 10:55:49 +0900, Marc Schellens
<m_*********@hotmail.com> wrote:
Is there a standard conforming way to check for the endian of
the current machine? Possibly at compile time?

I am thinking of something like:

union endian
{
char c[2];
unsigned int i;
};

endian e; // can it be initialized directly?
e.i=1;

bool BigEndian()
{
return (e.c[0] == 0);
}

But this is not at compile time.
No, AFAIK standard C++ does not deal with big endian/little endian
issues. Most applications which need to support both (e.g. parsing of
TIFF image files) will rely on the "magic numbers" in the header of
such files and react accordingly.

Have you checked the Boost libraries? Perhaps there is something in
there.

Further:
Is it guaranteed:
that an int has at least two bytes?
No. An "int" has to be at least large enough for "the natural size for
integer arithmetic on a given machine".

There is also no standard definition of the number of bits in a
"byte". There is a standard header file <limits> which has constants
such as CHAR_BIT (or CHAR_BITS, I forgot which is correct), INT_MIN,
INT_MAX, SHRT_MAX etc. which might help you wite portable code. These
provide information consistent with the platform on which the
application has been compiled, however; if you need the information at
run-time, you'll need to do something OS-specific.
that the bit pattern for zero and one are the same
for char and int?


Since char is also an integral type, they would have to be (except for
the number of bits). Otherwise the shift operators '<<', '>>' and
binary operators '&', '^' etc. wouldn't work consistently.
--
Bob Hairgrove
rh**************@Pleasebigfoot.com
Jul 19 '05 #2
"Marc Schellens" <m_*********@hotmail.com> wrote in message
news:3F**************@hotmail.com...
| Is there a standard conforming way to check for the endian of
| the current machine? Possibly at compile time?

There is no way to check for endianess at compile time
- except using vendor-specific preprocessor identifiers.

For the runtime test, your code is ok if you don't want
to bother with exotic systems with mixed endianness etc.
union endian
{
char c[2];
unsigned int i;
};

endian e; // can it be initialized directly?
e.i=1;

bool BigEndian()
{
return (e.c[0] == 0);
}


This could be simplified into:
bool BigEndian()
{
unsigned short s = 0x0001;
return ! *(unsigned char*)&s;
}
Regards,
--
Ivan Vecerina, Dr. med. <> http://www.post1.com/~ivec
Soft Dev Manger, xitact <> http://www.xitact.com
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 19 '05 #3

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. ...
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 }
14
by: ThazKool | last post by:
I want to see if this code works the way it should on a Big-Endian system. Also if anyone has any ideas on how determine this at compile-time so that I use the right decoding or encoding...
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 }
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....
3
RRick
by: RRick | last post by:
This was a question that showed up in a job interview once. (And to answer your next question: No, I didn't :)) Write a subroutine that returns a bool on whether a system supports big endian...
6
by: Javier | last post by:
Hello people, I'm recoding a library that made a few months ago, and now that I'm reading what I wrote I have some questions. My program reads black and white images from a bitmap (BMP 24bpp...
23
by: guthena | last post by:
Write a small C program to determine whether a machine's type is little-endian or big-endian.
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."); }
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...
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.