X hits on this document

161 views

0 shares

0 downloads

0 comments

27 / 62

27

void CheckEndian() {

 union {

   char charword[4];

   unsigned int intword;

 } check;

 check.charword[0] = 1;

 check.charword[1] = 2;

 check.charword[2] = 3;

 check.charword[3] = 4;

#ifdef BIG_ENDIAN

 if (check.intword != 0x01020304) {/* big */

   cout << "ERROR: Host machine is not Big Endian.\nExiting.\n";

   exit (1);

 }

#else

#ifdef LITTLE_ENDIAN

 if (check.intword != 0x04030201) {/* little */

   cout << "ERROR: Host machine is not Little Endian.\nExiting.\n";

   exit (1);

 }

#else

 cout << "ERROR: Host machine not defined as Big or Little Endian.\n";

 cout << "Exiting.\n";

 exit (1);

#endif // LITTLE_ENDIAN

#endif // BIG_ENDIAN

}

Another union example

Document info
Document views161
Page views161
Page last viewedThu Dec 08 02:41:46 UTC 2016
Pages62
Paragraphs993
Words3163

Comments