What is endianness ?
Endianness is the way bytes are stored in computer's memory address.
In a 32 bits machine an integer is 32 bits ie 4 bytes as bits are divided in group of 8 bits.so
For example,
suppose we have a 32 bit quantity, written as ox90AB12CD16, which is in hexadecimal.
So, the 4 bytes are: 90, AB, 12, CD where each byte requires 2 hex digits.
There are two ways to store this in memory(as memory is considered as array of bytes).
1. Big Endian
In big endian, the most significant byte is stored in the smallest address. Here's how it would look:
Address Value
1000 90 you may also like linked list questions asked in recent interviews 2014
1001 AB
1002 12
1003 CD
ie 90, AB, 12, CD
If it is an integer say 1 then in a big endian machine it will be stored as 00 00 00 01
2. Little Endian
In little endian, you store the least significant byte in the smallest address. Here's how it would look:
Address Value
1000 CD You may also like user defined string functions asked in recent walkins 2014
1001 12
1002 AB
1003 90
i.e. CD 12 AB 90
If it is an integer say 1 then in a little endian machine it will be stored as 01 00 00 00
How to find the endianness of a machine?
Below code tells how to know the Endianness of a machine.
#include<stdio.h>
int main()
{
In the above program,
a character pointer c is pointing to an integer i.
Since size of character is 1 byte when the character pointer is de-referenced it will contain only first byte of integer.
So,
if machine is little endian then *c will be 1 (because last_byte/list_signifant_byte is stored first/in lowes memory address in little endian. ) 00 00 00 01
and
if machine is big endian then *c will be 0 (because fisrt byte is stored firs) 01 00 00 00
Output on a ubuntu terminal is "little endian machine". means 1 was stored as 01 00 00 00
so if you print ox01234567 in little endian then it is stored as 67 45 23 01
Examples:
Generally Intel based processors are little endians. ARM processors were little endians.
Current generation ARM processors are bi-endian.
Motorola 68K processors are big endians. PowerPC (by Motorola) and SPARK (by Sun)
thesse are most frequently asked interview questions in walk-ins and current interviews regarding eandian ness of a mchine.
Why endianness is used or why the ordering of bytes matter?
endian ness concept is must for network programs like socket programs as it is used by various types of machine and the program should work as desired in all the machines irrespective of it is a little or big endian machine.
There is also a concept of bi-endian hardware.
Endianness is the way bytes are stored in computer's memory address.
In a 32 bits machine an integer is 32 bits ie 4 bytes as bits are divided in group of 8 bits.so
For example,
suppose we have a 32 bit quantity, written as ox90AB12CD16, which is in hexadecimal.
So, the 4 bytes are: 90, AB, 12, CD where each byte requires 2 hex digits.
There are two ways to store this in memory(as memory is considered as array of bytes).
1. Big Endian
In big endian, the most significant byte is stored in the smallest address. Here's how it would look:
Address Value
1000 90 you may also like linked list questions asked in recent interviews 2014
1001 AB
1002 12
1003 CD
ie 90, AB, 12, CD
If it is an integer say 1 then in a big endian machine it will be stored as 00 00 00 01
2. Little Endian
In little endian, you store the least significant byte in the smallest address. Here's how it would look:
Address Value
1000 CD You may also like user defined string functions asked in recent walkins 2014
1001 12
1002 AB
1003 90
i.e. CD 12 AB 90
If it is an integer say 1 then in a little endian machine it will be stored as 01 00 00 00
How to find the endianness of a machine?
Below code tells how to know the Endianness of a machine.
#include<stdio.h>
int main()
{
unsigned int i = 1;
char *c = (char*)&i;/*a character pointer c is pointing to an integer i*/
if (*c) /*Since size of character is 1 byte when the character pointer is de-referenced it will contain only first byte of integer.*/
printf("Your machine is Little endian.\n");//01 00 00 00 because 01 means 1 and true
else
printf("It is a Big endian Machine.\n");//00 00 00 01
return 0;
}
Explanation:In the above program,
a character pointer c is pointing to an integer i.
Since size of character is 1 byte when the character pointer is de-referenced it will contain only first byte of integer.
So,
if machine is little endian then *c will be 1 (because last_byte/list_signifant_byte is stored first/in lowes memory address in little endian. ) 00 00 00 01
and
if machine is big endian then *c will be 0 (because fisrt byte is stored firs) 01 00 00 00
Output on a ubuntu terminal is "little endian machine". means 1 was stored as 01 00 00 00
so if you print ox01234567 in little endian then it is stored as 67 45 23 01
Examples:
Generally Intel based processors are little endians. ARM processors were little endians.
Current generation ARM processors are bi-endian.
Motorola 68K processors are big endians. PowerPC (by Motorola) and SPARK (by Sun)
thesse are most frequently asked interview questions in walk-ins and current interviews regarding eandian ness of a mchine.
Why endianness is used or why the ordering of bytes matter?
endian ness concept is must for network programs like socket programs as it is used by various types of machine and the program should work as desired in all the machines irrespective of it is a little or big endian machine.
There is also a concept of bi-endian hardware.
No comments:
Post a Comment