Endianness facts for kids
In computing, endianness is about the order in which a computer stores or sends groups of information called "bytes." Imagine a computer word, which is like a number made of several bytes. Endianness decides if the most important part of that word (the "big end") or the least important part (the "little end") comes first.
Computers store information in tiny pieces called bits. Eight bits together make a byte. Each byte has a special number called its address, which helps the computer find it. When you have a bigger piece of information, like a 32-bit word, it's made of four bytes.
There are two main ways computers arrange these bytes:
- Big-endian (often shortened to BE): The computer puts the most important byte (like the first digit of a big number) at the lowest memory address. This is similar to how we read numbers from left to right.
- Little-endian (often shortened to LE): The computer puts the least important byte (like the last digit of a big number) at the lowest memory address. This is like reading a number backwards.
Both ways are used a lot in computers today. When new computer designs are made, the choice of endianness might seem random at first. But once chosen, it usually sticks around so that newer versions can still work with older ones.
Big-endian is very common for sending data over networks, like the internet. It's often called network order. This means the most important byte is sent first. On the other hand, little-endian is usually found inside the main parts of computers, like the CPU (the "brain" of the computer). Many file formats can use either order, or they might even tell you which order they are using.
Some computers are bi-endian. This means they can switch between big-endian and little-endian depending on what they need to do. Other less common orders are sometimes called middle-endian or mixed-endian.
Contents
Where the Name Comes From
The terms "big-endian" and "little-endian" were first used in computer science by Danny Cohen in 1980. He got the idea from a famous book called Gulliver's Travels, written by Jonathan Swift in 1726.
In Swift's story, there are two groups of people who are fighting. One group believes you should break a boiled egg from the big end, and the other believes you should break it from the little end. It's a silly reason to fight, but it shows how small differences can cause big problems. In the same way, computers can have "fights" over whether to read the "big end" or "little end" of data first!
How Endianness Works
Computer memory is like a long list of storage spots. Each spot holds one byte of information and has its own unique memory address. If a computer has n bytes of memory, the addresses go from 0 up to n minus 1.
Computers often use data that is bigger than one byte, like a number that takes up four bytes. Usually, the address of this multi-byte data is the address of its very first byte (the one with the lowest address).
The "significance" of a byte is how important it is to the overall value. Think of a number like 123. The '1' is the most significant digit, and the '3' is the least significant. Endianness decides how these significant bytes are placed in memory.
Numbers in Memory
Computers mostly use a number system based on 2 (binary) to store and work with numbers. In this system, each digit's value depends on its position. These positions can be stored in memory in two main ways:
- Big-endian: The most important part of the number is stored at the lowest memory address.
- Little-endian: The least important part of the number is stored at the lowest memory address.
So, the "end" in "big-endian" or "little-endian" refers to which "end" of the number (the big, important end or the little, less important end) is placed at the lowest memory address.
Text in Memory
When computers compare text strings (like words in a dictionary), they usually do it by looking at the first character first, then the second, and so on. Most computers that can do this quickly are big-endian or mixed-endian.
Numbers written as text (like "123" instead of the actual number 123) are almost always stored with the most important digit first, which is similar to big-endian, no matter which way the text is read.
Seeing Bytes in Order
When you look at computer memory, like in a "hex dump" (a way to see raw data), little-endian numbers can look a bit strange. They appear backwards because the least important part is on the left.
For example, if you store the word "JOHN" as a 32-bit number:
- On a big-endian computer, it would look like "J O H N" in memory, which is easy to read.
- On a little-endian computer, it would look like "N H O J" in memory, which is backwards!
Swapping Bytes
Sometimes, you need to change the endianness of data, for example, when moving a file from a big-endian computer to a little-endian one. This is called byte swapping.
Many computer languages and processors have special tools or instructions to do this quickly. For example, some CPUs have a `bswap` instruction that can quickly reverse the order of bytes in a word. This helps programs work correctly on different types of computers without needing a lot of changes.
Important Things to Consider
Easy Access to Parts of Data
On most computers, the address of a multi-byte value is the address of its first byte (the one with the lowest address). Little-endian systems have a cool feature: you can read a small part of a number (like just the first byte) from the same memory address, and it will still have the correct value.
For example, if a 32-bit memory spot holds the value `0000004A` (in little-endian), you can read just the first byte at that address and get `4A`. This can sometimes make programming easier, especially for low-level tasks or when talking directly to hardware.
How Calculations Work
Some math operations, like addition and subtraction, naturally start with the least important digits and then carry over to the more important ones. On little-endian computers, where the least important byte is at the lowest address, these operations can be slightly simpler to set up.
Other operations, like comparing numbers or division, start with the most important digits. For these, big-endian computers might have a slight advantage. However, modern powerful processors usually handle multi-byte numbers very quickly, so the difference in speed is often tiny.
Computer Hardware and Endianness
Many older and current computer processors use big-endian memory. For example, the IBM System/360 and its newer versions use big-endian. The Motorola 68000 series processors also use big-endian.
On the other hand, the PDP-11 and its successor, the VAX, use little-endian. Most Intel processors, including the common x86 and x86-64 chips found in many desktop computers and laptops today, are little-endian. Many other popular processors like the MOS Technology 6502 (used in old Nintendo and Apple computers) and Zilog Z80 (used in Game Boy) are also little-endian.
Bi-endian Processors
Some advanced processors are bi-endian. This means they can be set to work in either big-endian or little-endian mode. This feature is useful because it can make it easier for these computers to work with different types of data, especially in networking.
Examples of bi-endian processors include PowerPC, SPARC, ARM, and MIPS. Often, the computer chooses its default endianness when it starts up.
For instance, IBM AIX and IBM i operating systems run in big-endian mode on Power ISA processors. However, Linux on Power processors has moved to little-endian mode to make it easier to use software originally made for x86 (little-endian) computers.
Some bi-endian CPUs can even change endianness for each individual instruction that loads or saves data from memory. This gives programmers a lot of flexibility.
Many processors also have special instructions to quickly convert a number in a register (a small, fast storage area in the CPU) from one endianness to the other.
Floating Point Numbers
Floating-point numbers are used to represent numbers with decimal points, like 3.14. While many processors use the same endianness for all types of data (integers and floating-point), some have different rules.
For example, some ARM processors store parts of double-precision floating-point numbers in a mixed way. Even though the IEEE 754 standard for floating-point numbers doesn't specify endianness, most modern computers use the same endianness for floating-point numbers as they do for integers. This makes it easier to share floating-point data between different machines.
Software and Endianness
Files and Filesystems
It's very important to know about endianness when you're trying to open a file or use a filesystem that was created on a computer with a different endianness. If you don't, the data might look like gibberish!
For example, Unicode text files can sometimes start with a special "byte order mark" (BOM). This mark tells the computer whether the file is big-endian or little-endian, so the computer knows how to read it correctly.
Many file formats, like TIFF image files, also have a special part in their header that tells the program which endianness was used to create the file. This way, a program can swap the bytes if needed to read the file correctly.
The File Allocation Table (FAT) file system, which is used on many USB drives and older computers, always uses little-endian order. So, if a big-endian computer wants to work with a FAT drive, it has to do byte-swapping.
Networking
When computers talk to each other over a network, they need to agree on the order of bytes. Many internet protocols use "network order," which is defined as big-endian. This means that when data is sent over the internet, the most important byte goes first.
However, not all network protocols use big-endian. For example, the Server Message Block (SMB) protocol, used for sharing files on Windows networks, uses little-endian.
To help programmers, there are special functions (like `htons` and `htonl`) that convert numbers from the computer's own endianness to network order, and vice versa. This makes it easier to write programs that can communicate over networks, no matter what kind of computer they are running on.
Images for kids
-
Gulliver's Travels by Jonathan Swift, the book where the term "endian" came from.
See also
In Spanish: Endianness para niños