Re: Detecting Intel / PowerPPC?



On 2006-03-02 06:44:11 -0500, Michael Ash <mike@xxxxxxxxxxx> said:

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.

Look at the routines in CFByteOrder.h. You can use those to load and conditionally swap values (ints or floats) without worrying about what processor you're on.

If you really want to roll your own (please don't), then you can use the __BIG_ENDIAN__ and __LITTLE_ENDIAN__ preprocessor defines.

Indeed, that's the good answer. The bad answer is

#include <stdio.h>

int main(void)
{
int x=1;
if ((int) (* (char *) &x))
printf("This is a little endian architecture\n");
else
printf("This is a big endian architecture\n");
}

But really, it's clever and fun. Go with what Michael said for good software!

-Bill

--
Bill Stevenson
Mac OS X Product Release Group
Apple Computer
bstevenson at remove this text dot apple dot com

.



Relevant Pages

  • Re: Code Review - is this code shit
    ... her unfinished book "the art of C programming". ... Under old K&R C, x would be int, which is correct type for the ... endian nature of the host computer (keep in mind that big endian and ...
    (comp.lang.c)
  • Re: Variation of bit-counting
    ... > These numbers appear as IP subnet masks. ... void initHighBitCount { ... int i, j; ... Of course, this all assumes you are little endian, and chars are 8 ...
    (comp.programming)
  • 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? ... int CheckEndianness ... rube-goldbergish solutions this question provokes. ...
    (comp.sys.mac.programmer.help)
  • Re: Endian Independence
    ... int endian() { ... you assume that there are only two possible "endian" ... When the least-significant portion is stored first, ... you should first determine how "wide" your compiler makes ...
    (comp.lang.c)

Loading