# Endianness: Understanding Big and Little Endian Byte Orders in Computer Memory

The endianness refers to the byte order used by your computer or microcontroller or a machine to read or write a single “machine word” in memory (32-bit machine’s word size is 32-bit and 64-bit machine’s word size is 64-bit ). In other words, The endian will decide how to stores multiple bytes in computer memory. It doesn’t mean, the order of bits inside a byte,  nor the way the computer reads an array of bytes or a file. It’s all about the order of the bytes of a word (multi-byte variable) in memory. Now we will move into Little Endian and Big Endian.

## Little Endian And Big Endian

### Little Endian

In little endian byte order, the least significant byte (LSB) is stored at the lowest memory address, and the most significant byte (MSB) is stored at the highest memory address.  Intel x86, Pentium are using this Little endian.

Thus, the little-endian byte order means, when the computer writes a word (Multi Byte) into memory, it begins by writing the Lowest byte to the lowest memory address and continues until it has written the highest byte to the highest memory address. It does this by writing subsequent and ascending memory addresses, no matter the endianness.

Let’s consider a 4-byte (32-bit) integer with the value `0x12345678`:

## Big Endian

In big endian byte order, the most significant byte (MSB) is stored at the lowest memory address, and the least significant byte (LSB) is stored at the highest memory address. This is also known as network byte order, because it is the format used in internet protocols like TCP/IP.

The big endian byte order means, when the computer writes a word (Multi Byte) into memory, it begins by writing the highest byte to the lowest memory address and continues until it has written the lowest byte to the highest memory address.

Using the same example of 4 bytes integer which we considered earlier – `0x12345678`:

Now we will see the example, then you will understand very easily.

## Importance of understanding Endianness:

It is crucial to understand endianness when working with different hardware architectures or when transferring data between systems. Incompatible endianness can cause data corruption or incorrect results. Most modern computer systems use either big or little endian, with some even supporting both through configurable settings.

## Example

Consider the number `0x11223344`. This number is written with hexadecimal digits (prefix “`0x`”). Its decimal value is `287454020`. Its consists of 4 bytes: `0x11``0x22``0x33` and `0x44`.

In this value LSB is `0x44` and MSB is `0x11`.

Now assume that the computer wants to write this number into memory beginning at address 100. This 4-byte value. So it will use the memory address 100, 101, 1o2, 103.

## Program to see memory arrangement:

Now we are going to write a program to see the memory arrangement on our computer. This program will print the memory address and content of our value.

endian_test.c

```#include <stdio.h>

int main(void)
{
unsigned int value = 0x12345678;
char *r = (char *) &value;
int i;

for(i=0; i<4; i++)
{
printf("Address of 0x%x = %d \n", r[i], &r[i]);
}
return 0;
}```

Build this code using the following command:

`gcc -o endian_test endian_test.c`

Now, running the executable which is generated will give us the following output:

Here, the MSB is at the higher memory location. Thus, this system is a Little Endian system.

### Program to test Endianness of a system:

```#include <stdio.h>

int main(void)
{
unsigned int value = 0x1;
char *byte = (char *) &value;

if (*byte == 1)
else
return 0;
}
```

This program declares an integer variable `num` with a value of 1 and creates a character pointer `byte` that points to the memory address of `num`. If the system is little endian, the LSB will be stored at the lowest memory address, and the value at `byte` will be 1. If the system is big endian, the MSB will be stored at the lowest memory address, and the value at `byte` will be 0. The program then checks the value at the `byte` pointer and prints the endianness accordingly.

### Program to Test Endianness using Unions:

We can use Unions in C to test the Endianness of a system:

```#include <stdio.h>
union check_endian
{
unsigned int value;
char r;
};

union check_endian endian;

int main(void)
{
endian.value = 0x1;

if (endian.r == 1)
printf("The system is Little Endian\n");
else
printf("The system is Big Endian\n");
return 0;
}```