Online Reference

 

 

PROGRAMMING
RAPID TABLES

 

 

    Home > Programming > Endianess

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 offsetData
0byte0
1byte1
2byte2
3byte3
Example

The hex number 0x0D0C0B0A will be represented in memory:

Address offsetData
00A
10B
20C
30D

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 offsetData
0byte3
1byte2
2byte1
3byte0
Example

The hex number 0x0D0C0B0A will be represented in memory:

Address offsetData
00D
10C
20B
30A

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 / ProcessorEndianess
Intel Pentium / x86 (PC)Little
ARMLittle / Big
PowerPCBig / Little
Motorola 68000Big
MIPSBig / Little
Sun SparcBig / Little
TI TMS320C6000Big / Little
Analog ADMC / SharcBig
JavaBig
NetworkBig
PC / WindowsLittle
Mac / OS-XBig / Little
LinuxBig / Little
UnixBig / 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 x

}

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

© 2006-2008 RapidTables.com