If you’re using autoconf, you can use the AC_C_BIGENDIAN
macro, which is fairly guaranteed to work (setting the WORDS_BIGENDIAN
define by default)
alternately, you could try something like the following (taken from autoconf) to get a test that will probably be optimized away (GCC, at least, removes the other branch)
int is_big_endian()
{
union {
long int l;
char c[sizeof (long int)];
} u;
u.l = 1;
if (u.c[sizeof(long int)-1] == 1)
{
return 1;
}
else
return 0;
}