source: sources/src/guess_endianness.cc @ 4

Last change on this file since 4 was 1, checked in by buchmann, 17 years ago

Initial import from CVS repository

File size: 588 bytes
Line 
1// CODE FROM WIKIPEDIA
2
3#define LITTLE_ENDIAN 0
4#define BIG_ENDIAN    1
5
6int machineEndianness()
7{
8   int i = 1;
9   char *p = (char *) &i;
10   if (p[0] == 1) // Lowest address contains the least significant byte
11      return LITTLE_ENDIAN;
12   else
13      return BIG_ENDIAN;
14}
15
16#include <stdio.h>
17
18int
19main (int argc, char** argv)
20{
21  printf ("#define ");
22  switch (machineEndianness()) {
23  case LITTLE_ENDIAN : 
24    printf ("little_endian\n");
25    break;
26  case BIG_ENDIAN : 
27    printf ("big_endian\n");
28    break;
29  default:
30    printf ("unknown_endian\n");
31    break;
32  }
33  return 0;
34}
35
Note: See TracBrowser for help on using the repository browser.