The 'Bits' Compact Format
The 'Bits' Field: Unpacking Compact Target Serialization in Block Headers
To keep the block header compact and save valuable storage space, the 32-byte (256-bit) target is not written to the header in its full layout. Instead, it is compressed into a highly efficient 4-byte (32-bit) floating-point-like representation stored in the bits field.
This guide details the structure of the bits field, the decompression formula used to rebuild the full 256-bit target, and a step-by-step parsing walkthrough.
📐 1. The Structure of the 4-Byte bits Field
The 4-byte bits field represents a floating-point number using a base-256 scientific notation. It is split into two components: an Exponent (1 byte) and a Coefficient (3 bytes, sometimes called the mantissa).
THE 4-BYTE 'BITS' LAYOUT
Bits: 0x1703a3d5 (4 Bytes)
1 Byte: Exponent 3 Bytes: Coefficient
┌──────────┐ ┌──────────────────────┐
│ 0x17 │ │ 0x03a3d5 │
└──────────┘ └──────────────────────┘
- Exponent (Byte 0): Defines the size (length) of the target array.
- Coefficient (Bytes 1-3): Defines the leading non-zero digits of the target array.
🧮 2. The Decompression Formula
To decompress bits into the full 256-bit target integer, nodes apply this mathematical equation:
$$\text{Target} = \text{Coefficient} \times 256^{(\text{Exponent} - 3)}$$
Edge Cases and Sign Flags:
- Sign Bit: If the highest bit of the coefficient is set (
0x80or higher), the target is mathematically interpreted as a negative number. However, since target hashes are strictly unsigned integers, this state is invalid. If the target coefficient begins with0x80or higher, nodes prepended an extra null byte0x00during compression, shifting the exponent value up by 1. - Overflows: If the calculated target exceeds the maximum allowable target ($\text{Target}{\text{max}}$), the target is clamped down to $\text{Target}{\text{max}}$ to preserve consensus rules.
🛠️ 3. Step-by-Step Decompression Walkthrough
Let's decompress the real block header bits value: 0x1703a3d5.
Step A: Extract the Exponent and Coefficient
Exponent=0x17(Decimal: $23$)Coefficient=0x03a3d5(Decimal: $238,549$)
Step B: Plug the Values into the Formula
$$\text{Target} = \texttt{0x03a3d5} \times 256^{(23 - 3)}$$ $$\text{Target} = \texttt{0x03a3d5} \times 256^{20}$$
Step C: Execute the Multiplication
In hexadecimal, multiplying by $256$ is equivalent to shifting the value left by two hex places (or adding two trailing 00 characters):
* $256^1 = \text{shift by } 1 \text{ byte} \rightarrow \text{adds } \texttt{00}$
* $256^{20} = \text{shift by } 20 \text{ bytes} \rightarrow \text{adds } 20 \text{ trailing } \texttt{00}$ bytes (40 hex characters)
- Take the coefficient:
03a3d5 - Pad left with zeroes to make it 32 bytes (64 hex characters) total: $$\texttt{000000000000000000000000000000000000000000000000000000000003a3d5}$$
- Shift left by 20 bytes (by moving the digits left and appending 20 trailing null bytes
\x00): $$\text{Target} = \texttt{00000000000000000003a3d50000000000000000000000000000000000000000}$$
This decompressed 256-bit target is used by the node's verification engine to evaluate incoming block header hashes.
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: