Wednesday, July 2, 2008

Write a function that if run on different systems that tells the endianess of the system

Write a single function that if run on different systems that tells if it is little endian or big endian.

/** @return True if this machine has little endian byte order */
bool isLittleEndian() {
int i = 1;
char *c = &i; // get the address of i

// true if the first byte contains the least significant byte
return (*c == 1);
}
Note: bit shifts and masking do not work because they should work the same way on every system.

No comments: