Little Endian vs. Big Endian
Little Endian vs. Big Endian
One of the most confusing aspects of Bitcoin data serialization is the way multi-byte numbers are stored in memory. This is known as Endianness. Bitcoin uses both "Big Endian" and "Little Endian" depending on what part of the protocol you are looking at.
1. What is Endianness?
Imagine you have the hex number 12 34 56 78. How should you write it in a file?
-
Big Endian (BE): Write it "Natural" order:
12 34 56 78. The "Big" end comes first. -
Little Endian (LE): Write it "Reverse" byte order:
78 56 34 12. The "Little" end comes first.
2. Why does Bitcoin use Little Endian?
Satoshi Nakamoto developed Bitcoin on x86 computers (Intel CPUs).
-
Intel CPUs are Little Endian internally.
-
To make the code faster and simpler, Satoshi chose to store many numbers (like transaction amounts, versions, and block heights) in the CPU's native Little Endian format.
3. The "Block Hash" Flip
This is the most common pitfall for new developers.
-
Internal Order (LE): This is how the computer sees the hash.
-
Display Order (BE): This is how block explorers (like Mempool.space) show the hash.
Example:
If a block hash starts with many zeros in an explorer:
00000000000000000005...
In the raw data of the block file, those zeros are actually at the End:
...05000000000000000000
4. What is flipped?
-
Flipped (Little Endian): TXIDs, Block Hashes, Transaction Amounts (Satoshis), Block Version numbers.
-
Natural (Big Endian): IP Addresses (in network packets), DER Signatures, and raw Private Keys.
5. Byte-Level vs. Bit-Level
Important: Endianness only applies to the Order of Bytes, not the order of bits inside a byte.
-
Hex
0x1Ais still0x1A. You don't flip the1and theA. -
You only flip the groups of two hex characters.
| Data Type | Endianness | Human Order | Raw Byte Order |
|---|---|---|---|
| Amount | Little | 100 |
64 00 00 00 |
| TXID | Little | Starts with 00 |
Ends with 00 |
| IP Address | Big | 1.2.3.4 |
01 02 03 04 |
In the next section, we will discuss Byte Serialization in Raw Transactions.
TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation: