Last change
on this file since 21 was
12,
checked in by buchmann, 16 years ago
|
Changes:
- Convert some includes like <stdlib.h> into <cstdlib>
- Remove some unused includes
|
File size:
587 bytes
|
Rev | Line | |
---|
[1] | 1 | // CODE FROM WIKIPEDIA |
---|
| 2 | |
---|
| 3 | #define LITTLE_ENDIAN 0 |
---|
| 4 | #define BIG_ENDIAN 1 |
---|
| 5 | |
---|
| 6 | int 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 | |
---|
[12] | 16 | #include <cstdio> |
---|
[1] | 17 | |
---|
| 18 | int |
---|
| 19 | main (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.