Skip to main content

Basics of Programming : Understanding Big Endian and Little Endian


Endianness refers to the sequential order in which bytes are arranged into larger numerical
values when stored in memory.

When we consider any multi byte value we know which is its LSB ( Least Significant Byte) and
which is MSB (Most Significant Byte) based on the arrangement of LSB or MSB in lower address
of the memory, the endianness is defined.

There are two ways in which multi byte values are stored in memory
  • Little Endian
  • Big Endian

Little Endian : If the lower byte (LSB) is stored in lower address of the memory this arrangement
is called as little endian

Big Endian : If the Higher byte (MSB) is stored in lower address of the memory this arrangement
is called as Big Endian.


For example,
 Consider a number 0x01020304, here least significant byte is 04 and most significant byte is 01.

This is how the arrangement in memory with two different endianness.

0x100
0x101
0x102
0x103
01
02
03
04
Big Endian

0x100
0x101
0x102
0x103
04
03
02
01
Little Endian


How do you find whether your machine is little endian or big endian ?

Below C Program can help you to know endianness of the machine


#include <stdio.h>
 
/* function to show bytes in memory, from location start to start+n*/
void show_mem_rep(char *start, int n) 
{
    int i;
    for (i = 0; i < n; i++)
         printf(" %.2x", start[i]);
    printf("\n");
}
 
/*Main function to call above function for 0x01234567*/
int main()
{
   int i = 0x01234567;
   show_mem_rep((char *)&i, sizeof(i));
   getchar();
   return 0;
}


When above program is run on little endian machine, gives “67 45 23 01” as output , while if it
is run on endian machine, gives “01 23 45 67” as output.


Comments

Post a Comment

Popular posts from this blog

Smart Pointer Basics in C++

    It is responsibility of the owner to free the memory which he is allocated dynamically. But the question is who actually owns the memory. Let us consider a function char * allocate (int size) {       char * ptr = new char[size];       return ptr; } here in this allocate function block of memory for characters is being allocated and the pointer to that memory location is returned. Now the question is the person who is calling this function if he doesnt store the pointer then what happens. If he stores and if he doesnt free then what happesns. In all these cases we can find memory leak. To avoid these kind of situations in our code, its better to use the inbuilt functions provided by c++ standard library. <memory> provides two smart pointers to help manage objects on the free store. 1. unique pointer 2. shared pointer The most basic use of these smart pointer is to avoid memory leak. Unique Pointer [unique_ptr] ...

Basics of Programming : Data Types

We have seen that variables are placeholders for data. When we say data, it can be a integral numbers like 3,5 or it can be a real number like 2.6 , 1.8. In computer science, we define different types of data which we can store in memory location. A data type in a programming language is set of data with predefined values. Examples include, integers, floating point, characters, Strings etc. Even though ultimatly everything in memory stored in terms of zeros and ones, it is not practical  for us to code interms of zeros and ones. So to help users with programming compilers provide programmers the data types, which are in readable data which we use in day today transactions. The data types are categorized based on the representation in memory or the size it occupies in memory. For example, integer takes 4 bytes ( depends on the compiler, some may take 2 bytes), float takes 4 bytes, a character takes 1 byte. There are two types of data types. System define...