How should I test endianness in C or C++?

  • From: "Delaunay Christophe" <christophe.delaunay@xxxxxxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Tue, 20 Jul 2010 09:28:32 +0200

Hi all,

Here is my piece of code:
------ BEGINNING OF CODE ------
/**
 *
 * This small program tests and explains endianness.
 *
 */

/* --- The needed header files --- */
#include <stdio.h> /* for input and output */
#include <inttypes.h> /* special integer types */

/* --- Given definition --- */
#ifdef WIN32 /* windows-like OS assumed */
#define ATTRIBUTE
#else /* linux-like OS assumed */
#define ATTRIBUTE       __attribute__((packed))
#endif /* windows or linux */

/* --- The byte divided into 8 bit fields --- */
typedef struct _bitfields {
#if __BYTE_ORDER == __BIG_ENDIAN
        uint8_t b0:1;
        uint8_t b1:1;
        uint8_t b2:1;
        uint8_t b3:1;
        uint8_t b4:1;
        uint8_t b5:1;
        uint8_t b6:1;
        uint8_t b7:1;
#elif __BYTE_ORDER == __LITTLE_ENDIAN
        uint8_t b7:1;
        uint8_t b6:1;
        uint8_t b5:1;
        uint8_t b4:1;
        uint8_t b3:1;
        uint8_t b2:1;
        uint8_t b1:1;
        uint8_t b0:1;
#endif /* big or little endian */
} ATTRIBUTE bitfields;

/* --- main --- */
int main(void) {
        uint8_t val=0;
        bitfields* bf = (bitfields*)(&val);
#if __BYTE_ORDER == __BIG_ENDIAN
        printf("This machine works in big endian.\n");
#elif __BYTE_ORDER == __LITTLE_ENDIAN
        printf("This machine works in little endian.\n");
#endif /* big or little endian */
        printf("To understand how bits are ordered into a byte.\n");
        printf("Give a value : ");
        scanf("%hhu", &val);
        while ( val != 0 ) {
                uint8_t n = bf->b0&1;
                printf("Given value = %d.\n", val);
                printf("val=%d,",n);
                n = bf->b1&1; printf("%d,",n);
                n = bf->b2&1; printf("%d,",n);
                n = bf->b3&1; printf("%d,",n);
                n = bf->b4&1; printf("%d,",n);
                n = bf->b5&1; printf("%d,",n);
                n = bf->b6&1; printf("%d,",n);
                n = bf->b7&1; printf("%d from b0 to b7.\n",n);
                printf("Give another value : ");
                scanf("%hhu", &val);
        } /* at this time, value is 0 */

        printf("Finished.\n");
        return 0;
} /* main */
------ END OF CODE ------

Problem: When I run this program, here is what I obtain:
------ BEGINNING OF OUTPUT ------
[delaunayc@rennxlxrda013 TestEndian]$ ./TestByteOrder
This machine works in big endian.
To understand how bits are ordered into a byte.
Give a value : 1
Given value = 1.
val=1,0,0,0,0,0,0,0 from b0 to b7.
Give another value : 2
Given value = 2.
val=0,1,0,0,0,0,0,0 from b0 to b7.
Give another value : 3
Given value = 3.
val=1,1,0,0,0,0,0,0 from b0 to b7.
Give another value : 4
Given value = 4.
val=0,0,1,0,0,0,0,0 from b0 to b7.
Give another value : 128
Given value = 128.
val=0,0,0,0,0,0,0,1 from b0 to b7.
Give another value : 130
Given value = 130.
val=0,1,0,0,0,0,0,1 from b0 to b7.
Give another value : 131
Given value = 131.
val=1,1,0,0,0,0,0,1 from b0 to b7.
Give another value : 0
Finished.
[delaunayc@rennxlxrda013 TestEndian]$
------ END OF OUTPUT ------

As you can see, this machine says that it works in big endian but
presents the byte order in little endian.

Therefore, I suspect my #if test to be bad but what did I do wrong
please?

The machine is a PC running Fedora 12. I compiled the above code with
GCC version 4.4.3.
Many thanks in advance. Have a nice day. Chris D
__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

Other related posts: