Endianness is the attribute of a system which decides the representation of a word in memory. Some system saves the most significant byte of a word at starting address while some stores the least significant byte at base address.it depends on your system endianness. On the basis of word representation in a system, Endianess divided into two category –
In the Little-endian system, a least significant bit(LSB) containing byte stores on the base address(0x00000000) of a word and rest of the byte stores from right to left at incrementing addresses.
Address | Little Endian System |
---|---|
0x00000000 | 0x0000000D |
0x00000001 | 0x0000000C |
0x00000002 | 0x0000000B |
0x00000003 | 0x0000000A |
Big Endian System-
The big Endian system is opposite to the little endian system.Big Endian System stores Most significant bit(MSB)containing byte at the very first location(0x00000000)and rest of the byte stores from left to right at incrementing address.
Address | Big Endian System |
---|---|
0x00000000 | 0x0000000A |
0x00000001 | 0x0000000B |
0x00000002 | 0x0000000C |
0x00000003 | 0x0000000D |
Example:
Little endian System: Intel x86-64 series processor, Zilog Z80, Altera Nios II.
Big Endian System: Motorola 68000 processor, IBM system etc.
Program to find your system Endianness-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { unsigned int word =0x0A0B0C0D; unsigned char *wptr; wptr=(unsigned char*)&word; if(*wptr==0x0A) printf("Your System Architecture is Big endian.\n"); else if(*wptr==0x0D) printf("Your System Architecture is little endian.\n"); else printf("Ohhh..Something Wrong With Your Architecture\n"); printf("Base Address Byte Is=0x0%x\n",*wptr); return 0; } |
if your system is Intel x86 or X64 then the output is:
1 2 |
Your System Architecture is little endian Base Address Byte Is=0x0D |
if your system is Motorola output is:
1 2 |
Your System Architecture is Big endian Base Address Byte Is=0x0A |
Suppose we have a system in which a Motorola processor device is working as a transmitter and Intel x86 as a receiver.
Big Endian Device (Motorola ) sent a word data 0x0A0B0C0D to your Little Endian Device. How your big-endian data treated by Little Endian device.
Address | Big Endian System | Little Endian System |
---|---|---|
0x00000000 | 0x0000000A | 0x0000000D |
0x00000001 | 0x0000000B | 0x0000000C |
0x00000002 | 0x0000000C | 0x0000000B |
0x00000003 | 0x0000000D | 0x0000000A |
so we can see that if we save big-endian data in the little endian system without conversion then transmit data will never match with received data.
So we have to convert big endian data in the little endian format before saving into little endian intel device otherwise you will receive reverse values that won’t make sense.
we can use following C code to convert big endian data into little endian –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { unsigned int bigEndianData=0x0A0B0C0D; // Big Endian Data unsigned int littleEndianByte[4]; unsigned int littleEndianData; littleEndianByte[3]= (bigEndianData<<24)&0xFF000000; littleEndianByte[2]= (bigEndianData<<8)&0x00FF0000; littleEndianByte[1]= (bigEndianData>>8) &0x0000FF00; littleEndianByte[0]= (bigEndianData>>24)&0x000000FF; littleEndianData=littleEndianByte[3] | littleEndianByte[2] | littleEndianByte[1] | littleEndianByte[0]; printf("Data Saved in Little Endian System=0x0%x\n",littleEndianData); return 0; } |
output :
1 |
Data Saved in Little Endian System=0x0D0C0B0A |
if you are getting data from the little Endian system then use below C code for conversion before saving into Big Endian system memory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { unsigned int littleEndianData=0x0D0C0B0A; unsigned int bigEndianByte[4]; unsigned int bigEndianData; bigEndianByte[3]= (littleEndianData<<24)&0xFF000000; bigEndianByte[2]= (littleEndianData<<8)&0x00FF0000; bigEndianByte[1]= (littleEndianData>>8) &0x0000FF00; bigEndianByte[0]= (littleEndianData>>24)&0x000000FF; bigEndianData= bigEndianByte[3] | bigEndianByte[2] | bigEndianByte[1] | bigEndianByte[0]; printf("Data Saved in Big Endian System=0x%x\n",bigEndianData); return 0; } |
output :
1 |
Data Saved in Big Endian System=0x0A0B0C0 |
Choice of system endianness depends on your system architecture.there is no advantage of one over other.Big Endian Format is commonly used in networking for data transmission while little endian is the favorite of microprocessor.so we can say that both endianness has their own significance.
Although I have tried to cover most of the part related to this topic your suggestion and comment are always welcome to improve this article. I also request you to send me if you find any error in this article .thank you so much for reading 🙂