Sunday, July 20, 2014

Little Endian vs Big Endian




From http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Data/endian.html
For example: 90, AB, 12, CD where each byte requires 2 hex digits.

Big Endian (BE) / Little Endian (LE) are two ways to organize multi-byte words.
Big Endian
In big endian, you store the most significant byte in the smallest address. 

AddressValue
100090
1001AB
100212
1003CD

Little Endian

In little endian, you store the least significant byte in the smallest address. 

AddressValue
1000CD
100112
1002AB
100390
In Java
http://mindprod.com/jgloss/endian.html
Java stores binary values internally and in files MSB (Most Significant Byte) first, i.e. high order part first. This is referred to as big-endian byte sex or sometimes network order. Java binary files, Java sockets and OpenType font files also use big-endian order.

Everything in Java binary format files is stored big-endian, MSB (Most Significant Bit) MSB first. This is sometimes called network order. This is good news. This means if you use only Java, all files are done the same way on all platforms.

We can check whether java.nio.ByteOrder.nativeOrder() is equal to ByteOrder.LITTLE_ENDIAN or ByteOrder.BIG_ENDIAN.


Which is Better?
Both formats have their advantages and disadvantages.

In "Little Endian" form, assembly language instructions for picking up a 1, 2, 4, or longer byte number proceed in exactly the same way for all formats: first pick up the lowest order byte at offset 0. Also, because of the 1:1 relationship between address offset and byte number (offset 0 is byte 0), multiple precision math routines are correspondingly easy to write.

In "Big Endian" form, by having the high-order byte come first, you can always test whether the number is positive or negative by looking at the byte at offset zero. You don't have to know how long the number is, nor do you have to skip over any bytes to find the byte containing the sign information. The numbers are also stored in the order in which they are printed out, so binary to decimal routines are particularly

http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Data/endian.html
Why is endianness so important? Suppose you are storing int values to a file, then you send the file to a machine which uses the opposite endianness and read in the value. You'll run into problems because of endianness. You'll read in reversed values that won't make sense.
Endianness is also a big issue when sending numbers over the network. Again, if you send a value from a machine of one endianness to a machine of the opposite endianness, you'll have problems. This is even worse over the network, because you might not be able to determine the endianness of the machine that sent you the data.
The solution is to send 4 byte quantities using network byte order which is arbitrarily picked to be one of the endianness (not sure if it's big or little, but it's one of them). If your machine has the same endianness as network byte order, then great, no change is needed. If not, then you must reverse the bytes.

Read full article from Dr. Bill's Notes on "Little Endian" vs. "Big Endian"

Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts