Introduction
Little Endian and Big endian are two ways of storing multi-byte data-types ( int, float, etc) in computers. There are 32 bits in the four bytes and 32 bits in the pattern, but a choice has to be made about which byte of memory gets what part of the pattern. There are two ways that computers commonly do this:
- Big Endian
- Little Endian
Big Endian
The most significant byte (Big End) of the data is placed at the byte with the lowest address. The rest of the data is placed in order in the memory. In other words the byte with the most significant bits is first stored , that is at the lowest memory address.
Little Endian
The least significant byte (Little End) of the data is placed at the byte with the lowest address. The rest of the data is placed in order in the memory. In other words the byte with the least significant bits is stored in the smallest memory address.
Example
In the below image, 32-bit pattern unsigned number 0x12345678 is stored at address 0x00000000. The most significant byte is 0x12 and the least significant is 0x78. Within a byte the order of the bits is the same for all computers.
![Little Endian Vs Big Endian](https://mymusing.co/wp-content/uploads/2020/04/little_big_endian.png)
Below is a sample C code that shows the byte representation of integer data type of the machine it is executed on.
#include <stdio.h> // Function to show bytes in memory void print_bytes(char *start, int numBytes) { for (int i = 0; i < numBytes; i++){ printf("Address : %p, Byte : %x\n", &start[i], start[i]); } } int main(){ int i = 0x12345678; print_bytes((char *)&i, sizeof(i)); return 0; } // Program Output Address : 0xffffcbcc, Byte : 78 Address : 0xffffcbcd, Byte : 56 Address : 0xffffcbce, Byte : 34 Address : 0xffffcbcf, Byte : 12