false
])Returns the following values:
- 0
- if the processor is little-endian.
- 1
- if the processor is big-endian.
- -1
- if the endianness cannot be determined.
It is called by
is_little_endian()
andis_big_endian()
.If verbose is
true
, messages are printed to standard output.This function has been adapted from Harbison, Samuel P., and Guy L. Steele Jr. C, A Reference Manual, pp. 163–164. This book has the clearest explanation of endianness that I've found so far.
This is the C++ code:
signed short System::get_endianness(const bool verbose) { union { long Long; char Char[sizeof(long)]; } u; u.Long = 1; if (u.Char[0] == 1) { if (verbose) cout << "Processor is little-endian." << endl << endl << flush; return 0; } else if (u.Char[sizeof(long) - 1] == 1) { if (verbose) cout << "Processor is big-endian." << endl << endl << flush; return 1; } else { cerr << "ERROR! In System::get_endianness():\n" << "Can't determine endianness. Returning -1" << endl << endl << flush; return -1; } }