Node: Endianness, Next: , Previous: System Information, Up: System Information



Endianness

signed short get_endianness ([const bool verbose = false]) Function
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() and is_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;
              }
          }
          

bool is_big_endian ([const bool verbose = false]) Function
Returns true if the processor is big-endian, otherwise false. If verbose is true, messages are printed to standard output.

bool is_little_endian ([const bool verbose = false]) Function
Returns true if the processor is little-endian, otherwise false. If verbose is true, messages are printed to standard output.