Byte Order

January 28, 2010

[Warning: This post is a backup recovery from my previous Wordpress blog. All content was automatically converted accessing a MySQL database using a Python script (details). Mostly are in Portuguese but if you are interest I can translate to English. If you found any problem dont’t hesitate to contact me in comments.]

In the book about Linux Kernel Programming the author Robert Love demonstrated a trick to check your hardware endianness.

int x = 1;
if (*(char *)&x == 1)
/* little endian */
else
/* big endian */

Using GCC you can use -mbig-endian or -mlittle-endian to generate appropriate endianess. Remember to check man pages section on your architecture (i.e: i386 and x86-64 don't implement this option whilst IA-64 and ARM yes)

One interesting point regards on byte swapping. Suppose that you have a file that starts with 0xaabbccdd referring to little endian and 0xddccbbaa referring to big endian (note that I took these values and order arbitrarily). In order to use one kind of byte order in your code we need check what endianness your file was generated and if necessary rectify. This C++ code exemplifies.

class Endian {

     int _byteSwapped;

public:

     Endian(unsigned long dw) {

        if (0xaabbccddL == dw) _byteSwapped = 1;
        else if (0xddccbbaaL == dw)  _byteSwapped = 0; 
        else throw "error";
   
     }

     long rectify(long dw) const {

        if (!_byteSwapped) return dw;
        char result[4] = {((char*) &dw)[3],((char*) &dw)[2],((char*) &dw)[1],((char*) &dw)[0]};
        return (*((long*) result));

     }
};

Another way to do byte swapping is using an macro. The following example swap two bytes:

#define SwapTwoBytes(data) ( (((data) >> 8) & 0x00FF) | (((data) << 8) & 0xFF00) )

One of the best practices is provide one software that will work correctly no matter which processor Endian-architecture the code is executed on, eliminating the need to rewrite the code. Intel has a interesting paper on Converting Endian-specific to Endian-neutral Code (pg. 15-16).