We must have heard the terms Little-Endian and Big-Endian many times in your engineering course. Let’s quickly recap the concept behind these words.
1. Little-Endian vs Big-Endian
These two terms are related to the direction of bytes in a word within CPU architecture.
Computer memory is referenced by addresses that are positive integers. It is “natural” to store numbers with the least significant byte coming before the most significant byte in the computer memory.
Sometimes computer designers prefer to use a reversed-order version of this representation.
The “natural” order, where the less significant byte comes before the more significant byte in memory, is called little-endian.
Many vendors like IBM, CRAY, and Sun preferred the reverse order that, of course, is called big-endian.
2. Example of Ordering
For example, the 32-bit hex value
0x45679812 would be stored in memory as follows:
Address 00 01 02 03 ----------------------------------- Little-endian 12 98 67 45 Big-endian 45 67 98 12
3. Endian-ness in Java
The difference in endian-ness can be a problem when transferring data between two machines.
Everything in Java binary files is stored in big-endian order. This is sometimes called network order. This means that if you use only Java, all files are done the same way on all platforms: Mac, PC, UNIX, etc. You can freely exchange binary data electronically without any concerns about endian-ness.
The problem comes when you must exchange data files with some program not written in Java that uses little-endian order, most commonly a program written in C. Some platforms use big-endian order internally (Mac, IBM 390); some uses little-endian order (Intel).
4. How to Know the Endian-ness
In Java, we can use the method ByteOrder.nativeOrder() to obtain the byte order used by the CPU. Below is the output on an Intel CPU.
ByteOrder byteOrder = ByteOrder.nativeOrder(); System.out.println(byteOrder); //LITTLE_ENDIAN
Java hides that internal endian-ness from us and gives us consistent results in all the platforms.
But in programming languages, where code reads the data directly from the memory areas using pointers, endian-ness could be an issue in cases where data is transferred from one machine to another machine and both machines have different endian-ness.
Happy Learning !!