Ferozeh dot Com
Expert advice on tech interviews !
about ferozeh

Ferozeh

Similar Questions

Question

Difficulty Level: 3

  • How can you find if a machine is big endian or small endian ?

Solution

Explanation Quality:5

Most of you would remember this question from your Networks class. Simply put Endianess is the order in which a machine stores data. Say I want to store the number 786001 in my computer memory as an integer. The number consists of six digits 7, 8, 6, 0, 0 and 1. The most significant digit being 7 and the least significant digit being 1. You can think of the most significant digit as the digit most important or greatest in value. Since 7 represents the hundred thousands digit thus it is the most significant digit.

Now number 786001 whose binary representation is 00010111111111001010001 will be stored inside a computer as bytes. Within a computer memory the binary representation of this number will be stored as following assuming we store the number in an integer variable of four bytes. Assume the addresses for the bytes start at 1.

00000000 00001011 11111110 01010001
Byte at address 1 Byte at address 2 Byte at address 3 Byte at address 4

However the attentive reader would question why couldn't the computer could have stored the same number just as well in the following manner?

01010001 11111110 00001011 00000000
Byte at address 1 Byte at address 2 Byte at address 3 Byte at address 4

The answer is that yes the computer can very well store the data this way and this is exactly the difference between the two kinds of endianess. The first way of storing data is where the most significant byte is stored at the lowest address i called Big Endian and the second way where the most significant byte is stored at the highest address is called little endian.

Given the above explanation all that one is required to do now is somehow figure out how a particular machine is storing data. The easiest way is to write a program. Say our machine stores integers as four bytes. If we are able to declare an integer in such a way that the first and the last bytes out of the four for the integer get distinct values then we have a way to test for endianess. Say we set the integer to 1 then 1 would be represented in the computer memory as follows:

00000000 00000000 00000000 00000001

Now the trick is simple if the machine is Big endian it will store 00000000 at the lowest memory address for the integer variable and if the machine is Little endian it will store 0000001 at the lowest memory address. All we have to do now is to check the value of the byte at the lowest memory address. If it is 1 we declare the machine to be big endian. If it is 0 we declare the machine small endian

The remaining issue is how to check the value at the lowest memory address. One way is to use a character pointer since character type is usually one byte and have it point to the integer variable.

char *ptr;
int dumb = 1;
ptr = &dumb;
if((*ptr) == 0)
{
cout<<"Machine is big endian"<<endl;
}
else{
cout<<"Machine is small endian"<<endl;
}

The effect of setting the character pointer equal to the interger variable is that now the character pointer is only pointing to the first byte of the integer variable. So all you do is to check if your character pointer's value is 0 or 1. If it is 0 you know that the machine is storing the most significant byte at the lowest address and it is Big endian otherwise the machine is storing the most significant byte at the highest address of the memory and is consequently Little Endian.




Previous Next