Re: Detecting Intel / PowerPPC?



Don Bruder <dakidd@xxxxxxxxx> wrote:
In article <WKANf.46723$d5.203491@xxxxxxxxxxxxxxx>,
"Mike" <no@xxxxxxxx> wrote:

Hi,

Is there a system call (C++) which I can use to check whether the program is
executing on an Intel machine or a PowerPC?

A couple of files which my application loads from the hard disk contain
floating
point numbers and then they need to be swapped (low / big endian) when
they're
loaded if the user is on an Intel Mac.

No clue about any sort of system call or built-in that would do it, but
it seems to me one could check by doing something like this in "plain
vanilla" C (which should work just fine in C++):

// return 0 if machine is BigEndian, 1 if LittleEndian
// Caveat: Assumes ints are four bytes. Alter accordingly
// if that's not true for your project.
int CheckEndianness(void)
[snip]

It's.... interesting.... to see how many bizarre and truly
rube-goldbergish solutions this question provokes. And not just here,
these things seem to pop up wherever the question is asked.

The aforementioned preprocessor defines are the right way to do it. If you
don't trust them (maybe because your code has to compile on MS-DOS 3.2 and
you're too lazy to set up your headers properly or something), there's no
reason to get any more complicated than the below:

int IsBigEndian(void) {
int chunk = 1;
return *(char *)&chunk;
}

--
Michael Ash
Rogue Amoeba Software
.



Relevant Pages

  • Re: Detecting Intel / PowerPPC?
    ... executing on an Intel machine or a PowerPC? ... int CheckEndianness ... Byte *fooPrime; ...
    (comp.sys.mac.programmer.help)
  • Re: Detecting Intel / PowerPPC?
    ... executing on an Intel machine or a PowerPC? ... point numbers and then they need to be swapped (low / big endian) when they're ... int main ... bstevenson at remove this text dot apple dot com ...
    (comp.sys.mac.programmer.help)
  • Re: Boxing and Unboxing ??
    ... two integers that are stored in reference types? ... to 'int' to add them - and that unboxes them. ... Is that actual Intel machine specific assembly language, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: printing all variables in union
    ... > what will be output of following program on INTEL machine ... > union s obj; ... integer 256=0x100 will be stored on a little endian machine ... (assuming size of int to be 4) ...
    (comp.lang.c)
  • Sockets Redirection problem
    ... I am trying to build a simple server that can execute local commands ... executing ls, cd, find and all of the normal unix utility programs. ... int execute; ...
    (comp.os.linux.development.apps)

Loading