Little and Big Endian

Endianess

Endianess is the byte order of the number in the computer's memory. The number can have any size of bits, but the most common numbers used are 32 bits (4 bytes) and 16 bits (2 bytes).

There are 2 main types of endianess:

Little endian

Little endian number is ordered from the least significant byte (LSB) in low memory address to the most significant byte (MSB) in high memory address.

Address offset Data
0 byte0
1 byte1
2 byte2
3 byte3
Example

The hex number 0x0D0C0B0A will be represented in memory:

Address offset Data
0 0A
1 0B
2 0C
3 0D

Big endian

Big endian number is ordered from the most significant byte (MSB) in low memory address to the least significant byte (LSB) in high memory address.

Address offset Data
0 byte3
1 byte2
2 byte1
3 byte0
Example

The hex number 0x0D0C0B0A will be represented in memory:

Address offset Data
0 0D
1 0C
2 0B
3 0A

Bits order

The order of the bits inside the byte is not changed when converting from little to big or from big to little.

Bi-endianess

Many processors allow to configure the endianess type to little endian or big endian.

Endianess table

Machine / Processor Endianess
Intel Pentium / x86 (PC) Little
ARM Little / Big
PowerPC Big / Little
Motorola 68000 Big
MIPS Big / Little
Sun Sparc Big / Little
TI TMS320C6000 Big / Little
Analog ADMC / Sharc Big
Java Big
Network Big
PC / Windows Little
Mac / OS-X Big / Little
Linux Big / Little
Unix Big / Little

Endian converter

TBD

Big / little endian conversion C code

The conversion is done, simply by swapping the bytes in memory for a 32 bit number.

Byte0 is swapped with byte3. Byte1 is swapped with byte2.

The conversion works both ways - from big to little and from little to big.

32 bits endian conversion of long number

unsigned long EndianSwap32(unsgined long x)

{

    unsigned long y=0;

    y += (x & 0x000000FF)<<24;

    y += (x & 0xFF000000)>>24;

    y += (x & 0x0000FF00)<<8;

    y += (x & 0x00FF0000)>>8;

    return y;

}

32 bits endian conversion of char array

void EndianSwap32(unsgined char c[])

{

    unsigned char tmp;

    tmp  = c[0];

    c[0] = c[3];

    c[3] = tmp;

    tmp  = c[1];

    c[1] = c[2];

    c[2] = tmp;

}

CPU endianess check C code

Here is a C code function to finds the endianess of the CPU:

typedef enum _endian {little_endian, big_endian} EndianType;

 

EndianType CheckCPUEndian()

{

    unsigned short x;

    unsigned char c;

    EndianType CPUEndian;

 

    x = 0x0001;;

    c = *(unsigned char *)(&x);

    if( c == 0x01 )

        CPUEndian = little_endian;

    else

        CPUEndian = big_endian;

 

    return CPUEndian;

}

 


See also

Write how to improve this page

PROGRAMMING
RAPID TABLES