How does C program determine if it runs on a Little-Endian or Big-Endian processor during RUN (not compilation time)?
The reason this should be a "run-time" check rather than a "complie-time" is because I am building a program in MAC OSX Universal Binary format using my MAC address with an Intel-CPU. It is expected that this program will work both on Intel processors and on Power-PC. those. through the universal binary format on the MAC, I want to create a program using Intel-CPU and run it under the PPC processor.
The logic in my program that needs to check the CPU is the function of changing the order of the host and network for 64-bit integers. Right now I have it blindly changing byte orders that work well on Intel-CPU, but break on PPC. Here's the C function:
unsigned long long
hton64b (const unsigned long long h64bits) {
return (
(
(unsigned long long)
( htonl((unsigned long) (h64bits & 0xFFFFFFFF)) )
) << 32
)
|
(
htonl((unsigned long) (((h64bits) >> 32) & 0xFFFFFFFF))
);
};
Best way to do it in a cross-platform way?
thanks
source
share