X hits on this document

276 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 views276
Page views276
Page last viewedMon Jan 23 19:38:43 UTC 2017
Pages62
Paragraphs993
Words3163

Comments