|
| 1 | +--- |
| 2 | +title: "Endianness" |
| 3 | +date: 2024-01-25T15:32:14Z |
| 4 | +lastmod: "2024-07-26" |
| 5 | +draft: false |
| 6 | +category: Taproot |
| 7 | +layout: TopicBanner |
| 8 | +order: 1 |
| 9 | +icon: "FaHashtag" |
| 10 | +images: |
| 11 | + [ |
| 12 | + "/bitcoin-topics/static/images/topics/thumbnails/taproot-roadmap-thumbnail.webp" |
| 13 | + ] |
| 14 | +--- |
| 15 | + |
| 16 | +Endianness refers to the order in which bytes are stored and read in a computer's memory. |
| 17 | + |
| 18 | +To understand it, imagine reading directions in different languages: while English Hex flows from left to right, Arabic Hex flows from right to left. |
| 19 | + |
| 20 | +Similarly, computers have two ways to store data: |
| 21 | +1. **Big-endian (BE):** Most significant byte first |
| 22 | +2. **Little-endian (LE):** Least significant byte first |
| 23 | + |
| 24 | +<div className="dark:hidden w-full rounded-xl overflow-hidden"> |
| 25 | + <SvgDisplay |
| 26 | + src="/bitcoin-topics/static/images/topics/transactions/technical-foundation/communication-endianness.svg" |
| 27 | + width="100%" |
| 28 | + height="auto" |
| 29 | + /> |
| 30 | +</div> |
| 31 | +<div className="hidden dark:block w-full rounded-xl overflow-hidden"> |
| 32 | + <SvgDisplay |
| 33 | + src="/bitcoin-topics/static/images/topics/transactions/technical-foundation/communication-endianness.svg" |
| 34 | + width="100%" |
| 35 | + height="auto" |
| 36 | + /> |
| 37 | +</div> |
| 38 | + |
| 39 | +## 1- Big-Endian |
| 40 | + |
| 41 | +Big-endian stores the **most significant byte** first. This is similar to how humans read numbers and Hex in most cases: starting with the most important information. |
| 42 | + |
| 43 | +Suppose we want to store the number **12345678** (hexadecimal: `0x00BC614E`) in memory. In **big-endian**, the bytes are stored in this order: |
| 44 | + |
| 45 | +<CodeSnippet |
| 46 | + language="Hex" |
| 47 | + code={` 00 BC 61 4E `} |
| 48 | +/> |
| 49 | + |
| 50 | +<div className="dark:hidden w-full rounded-xl overflow-hidden"> |
| 51 | + <SvgDisplay |
| 52 | + src="/bitcoin-topics/static/images/topics/transactions/technical-foundation/bigendian.jpg" |
| 53 | + width="100%" |
| 54 | + height="auto" |
| 55 | + /> |
| 56 | +</div> |
| 57 | +<div className="hidden dark:block w-full rounded-xl overflow-hidden"> |
| 58 | + <SvgDisplay |
| 59 | + src="/bitcoin-topics/static/images/topics/transactions/technical-foundation/bigendian.jpg" |
| 60 | + width="100%" |
| 61 | + height="auto" |
| 62 | + /> |
| 63 | +</div> |
| 64 | + |
| 65 | + |
| 66 | +- **Most significant byte** (`00`) is stored at the lowest memory address (00). |
| 67 | +- **Least significant byte** (`4E`) is stored at the highest address (03). |
| 68 | + |
| 69 | +Big-endian is considered more "human-readable" because the data is stored in the order we naturally read it. |
| 70 | + |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +## 2. Little-Endian |
| 75 | + |
| 76 | +Little-endian stores the **least significant byte** first. This might feel counterintuitive to humans but is more efficient for modern processors. |
| 77 | + |
| 78 | +Using the same number **12345678** (`0x00BC614E`), here’s how it looks in **little-endian**: |
| 79 | + |
| 80 | +<CodeSnippet |
| 81 | + language="Hex" |
| 82 | + code={` 4E 61 BC 00 `} |
| 83 | +/> |
| 84 | + |
| 85 | +<div className="dark:hidden w-full rounded-xl overflow-hidden"> |
| 86 | + <SvgDisplay |
| 87 | + src="/bitcoin-topics/static/images/topics/transactions/technical-foundation/littleendian.png" |
| 88 | + width="100%" |
| 89 | + height="auto" |
| 90 | + /> |
| 91 | +</div> |
| 92 | +<div className="hidden dark:block w-full rounded-xl overflow-hidden"> |
| 93 | + <SvgDisplay |
| 94 | + src="/bitcoin-topics/static/images/topics/transactions/technical-foundation/littleendian.png" |
| 95 | + width="100%" |
| 96 | + height="auto" |
| 97 | + /> |
| 98 | +</div> |
| 99 | + |
| 100 | + |
| 101 | +- **Least significant byte** (`4E`) is stored at the lowest memory address (00). |
| 102 | +- **Most significant byte** (`00`) is stored at the highest address (03). |
| 103 | + |
| 104 | +This "reversal" is common in Bitcoin's internal data representation. |
| 105 | + |
| 106 | + |
| 107 | +## 3. Endianness in Bitcoin |
| 108 | + |
| 109 | +In Bitcoin, **little-endian** is the standard for storing most data, like transaction IDs, block headers, and amounts. However, when this data is displayed to humans (e.g., in block explorers), it is often converted to **big-endian** for readability. |
| 110 | + |
| 111 | +<TransactionCreation enabledFields={["version", "locktime", "amount", "sequence"]} /> |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +## Bitcoin Transaction Example |
| 116 | + |
| 117 | +Let's say a transaction output amount is **12345678 satoshis**. In Bitcoin: |
| 118 | +- This value is stored as a **64-bit integer** (8 bytes) in **little-endian** format. |
| 119 | +- To humans, the hexadecimal representation would look like this in **big-endian**: |
| 120 | + |
| 121 | +<CodeSnippet |
| 122 | + language="Hex" |
| 123 | + code={` 00 00 00 00 00 BC 61 4E `} |
| 124 | +/> |
| 125 | + |
| 126 | +- In **little-endian**, this is reversed: |
| 127 | + |
| 128 | +<CodeSnippet |
| 129 | + language="Hex" |
| 130 | + code={` 4E 61 BC 00 00 00 00 00 `} |
| 131 | +/> |
| 132 | + |
| 133 | + |
| 134 | +If you were decoding raw Bitcoin transaction data, you'd need to reverse the byte order to understand the values correctly. |
| 135 | + |
| 136 | +--- |
| 137 | + |
| 138 | +## Why Does Bitcoin Use Little-Endian? |
| 139 | + |
| 140 | +Bitcoin uses **little-endian** because Satoshi developed it on a little-endian computer. |
| 141 | + |
| 142 | +Most modern CPUs are little-endian and the network protocols typically use big-endian, which can create a mismatch: |
| 143 | +- **Big-endian** is used for network communication (called **network byte order**). |
| 144 | +- **Little-endian** is used for internal storage in Bitcoin. |
| 145 | + |
| 146 | +This duality requires developers to frequently convert between the two formats when working with Bitcoin data. |
| 147 | + |
| 148 | +--- |
0 commit comments