Howdy!
I’m sure that most of the developers have met the terms “big endian” or “little endian”. Originally these two terms were used by Jonathan Swift in his Gulliver’s travels. Briefly people from Lilliput were cracking boiled eggs from the small end (little endians) while the inhabitans of the rival kingdom of Blefuscu were craking the eggs from the big end (big endians); but what do they mean in computing? They describe the way data with a size bigger than 1 byte, is memorized.
A computer using big endian starts memorizing from the most significant byte to the least significant one, while little endian starts memorizing from the least significant byte and ends with the most significant one. Let’s make an example with the hexadecimal number 0x12AB:
Endianness | Lowest address | Highest address |
---|---|---|
Big endian | 12 | AB |
Little endian | AB | 12 |
If we take into account a double word like 0x1234567 the order will be
Endianness | Lowest address | Middle byte 1 | Middle byte 2 | Highest address |
---|---|---|---|---|
Big endian | 01 | 23 | 45 | 67 |
Little endian | 67 | 45 | 23 | 01 |
So, when programming at low level whatever language you are using, the first thing to do is trying to understand which is the endianness of the machine; to help you do so, running this C++ snippet will let you know your machine endianness:
#include <iostream> using namespace std; int main() { int i=1; if((*(char*)&i)==1) cout<<"The machine is little endian."<<endl; else cout<<"The machine is big endian."<<endl; return 0; }
I hope this will help you!
Enjoy!