Is my Machine Big or Little Endian?
QUESTION: People talk about "little indian" and "big indian" machines. What in the world are they talking about!?
ANSWER: Uh, well, actually the word is not "indian", but "endian". It refers to the way the computer stores the bytes of a multiple byte integer number. Some machines store, for example, a two-byte integer with the least significant byte first, followed by the most significant byte. These machines are called little endian machines. Other machines store a two-byte integer with its most significant byte first, followed by it least significant byte. These machines are called big endian machines. Most UNIX machines are big endian. Whereas most PCs are little endian machines.
Integers (either two-byte or four-byte) are also said to be either network byte ordered, or host byte ordered. Network byte ordering is big endian and host byte ordered is little endian.
If you are not sure which endian your machine is, you can conduct this test from within IDL:
little_endian = (BYTE(1, 0, 1)) IF (little_endian) THEN $ Print, "I'm little endian." ELSE $ Print, "I'm big endian."
Why should the endian nature of your machine matter to you? Well, most of the time it won't. But it will matter if you try to read data files that were created on a machine that is of a different endian nature from your machine. Since these machines will have their bytes ordered diffently from what your machine expects, the data values you read will be different from what you expect.
For example, suppose the value 10405 is written as a two byte integer on a little endian machine. The binary representation of this number is this, with the most significant byte shown on the right:
0 0 1 0 1 0 0 0 1 0 1 0 0 1 0 1
If you read this value on a big endian machine, the number will be represented with the bytes swapped, or as this:
1 0 1 0 0 1 0 1 0 0 1 0 1 0 0 0
This number is the binary representation of the number 42280. So, you see, you need to know the endian nature of the machine that wrote the data as well as the endian nature of the machine that reads the data. If the machines are different, the bytes must be swapped to obtain the same values on both machines.
IDL makes this fairly easy to do with the ByteOrder and Swap_Endian functions. But it became even easier in IDL 5.1 with the introduction of the keywords Swap_Endian, Swap_If_Big_Endian, and Swap_If_Little_Endian. These keywords can be used on both the Open and Read procedures. This makes it quite easy to write code that can work on many different machines, if only you know the endian nature of the machine that created the data.
Copyright © 1998 David W. Fanning
Last Updated 9 September 1998