TECHNICAL_DOC // GENERAL / COMPACT-SIZE
COMPACT
SIZE
SIZE
CompactSize (also called VarInt) is Bitcoin's variable-length integer encoding
for expressing counts and lengths in serialized data. It uses 1, 3, 5, or 9 bytes depending
on the value, avoiding fixed-width waste for small values while supporting values up to
2^64−1. It appears before every list in a transaction: input count, output count, script
length, and witness item count.
ENCODING_TABLE
COMPACTSIZE ENCODING RULES
Value range Bytes Encoding
─────────────────────────────────────────────────
0x00 – 0xFC 1 byte value directly
e.g. 5 → 05
0xFD – 0xFFFF 3 bytes 0xFD + uint16 LE
e.g. 500 → FD F4 01
0x10000 – 0xFFFFFFFF 5 bytes 0xFE + uint32 LE
e.g. 70000 → FE 70 11 01 00
0x100000000 – 2^64-1 9 bytes 0xFF + uint64 LE
(theoretical max)
First byte tells you the total length:
0x00–0xFC → read 1 byte total (done)
0xFD → read next 2 bytes as uint16
0xFE → read next 4 bytes as uint32
0xFF → read next 8 bytes as uint64
Where CompactSize Appears in Transactions
SERIALIZATION
Every list or variable-length field in a Bitcoin transaction is preceded by a CompactSize integer indicating how many items or bytes follow.
Raw transaction parse order:
[4 bytes] version (LE uint32)
[varint] input count ← CompactSize
for each input:
[32 bytes] prev txid
[4 bytes] prev output index
[varint] scriptSig length ← CompactSize
[N bytes] scriptSig
[4 bytes] sequence
[varint] output count ← CompactSize
for each output:
[8 bytes] value (satoshis, LE)
[varint] scriptPubKey length ← CompactSize
[N bytes] scriptPubKey
[4 bytes] locktime
SegWit transactions additionally include:
[varint] witness stack items ← CompactSize (per input)
[varint] witness item length ← CompactSize (per item)
TERMINOLOGY_INDEX
CompactSize
Bitcoin's variable-length integer encoding. Also called VarInt. Uses 1, 3, 5, or 9 bytes.
VarInt
Alternative name for CompactSize. Used in Bitcoin Core's source code and documentation.
0xFD prefix
Signals a 3-byte CompactSize: the following 2 bytes are a little-endian uint16 (range 253–65535).
0xFE prefix
Signals a 5-byte CompactSize: the following 4 bytes are a little-endian uint32.
INTERACTIVE — TRY IT YOURSELF
INTERACTIVE_DOC // GENERAL / COMPACT-SIZE
COMPACT SIZE
INTERACTIVE
INTERACTIVE
Every list and variable-length field in a Bitcoin transaction is preceded by a
CompactSize integer telling parsers how many items follow. Its job:
pack small numbers into one byte, but support up to
2⁶⁴-1 when needed.
This page lets you watch the encoding choose its tier — type 252
(one byte), then 253 (suddenly three bytes). The boundaries are where
every parser bug lives.
§ 1 — THE 4-TIER ENCODING
CompactSize is one of four lengths: 1, 3, 5, or 9 bytes. The first
byte either is the value (if < 0xFD) or signals which wider encoding
follows. Type any number below — watch which tier it lands in.
INTERACTIVE — VALUE → ENCODED BYTEStype a number
—
Watch the cliffs: 252 → 1 byte, 253 → 3 bytes (the prefix
0xFD
plus 2 payload bytes). The encoded length doubles+1 at each tier, never grows by
one byte. This is how a length-prefix self-describes its own width.
§ 2 — TIER MAP (WHERE DOES THE VALUE LAND?)
Same number, different visualization. The four tiers and their ranges, with a marker
showing where the current value sits. Useful when reasoning about size budgets — for
example: if you build a transaction with 250 inputs, your input-count CompactSize is
1 byte. Add one more input → suddenly 3 bytes for the count.
INTERACTIVE — TIER MAPsyncs with input above
TIER 1 / 1B
0 – 252 (0x00 – 0xFC) · single byte = the value itself
—
TIER 2 / 3B
253 – 65,535 · prefix 0xFD + uint16 LE
—
TIER 3 / 5B
65,536 – 4,294,967,295 · prefix 0xFE + uint32 LE
—
TIER 4 / 9B
4,294,967,296 – 2⁶⁴-1 · prefix 0xFF + uint64 LE
—
§ 3 — DECODE FROM A BYTE STREAM
The reverse direction. A parser sees a byte stream — the first byte tells it whether
to read 1, 3, 5, or 9 bytes. Paste raw transaction bytes below; the page reads the
first CompactSize and shows you both the value and how many bytes were consumed
(highlighted). The remaining bytes are what the parser would parse next.
INTERACTIVE — DECODERpaste hex stream
—
§ 4 — WHERE COMPACTSIZE APPEARS IN BITCOIN
Real fields, real numbers. Each row below shows a field type, a typical value, the
CompactSize bytes that would encode it, and which tier it lands in. The pattern: most
transactions live in tier 1 (one byte). Tier 4 is theoretical — Bitcoin has never
needed it.
INTERACTIVE — REAL-WORLD CONTEXTStap any row to load
TERMINOLOGY_INDEX
CompactSize
Bitcoin's variable-length integer encoding. Also called VarInt. 1, 3, 5, or 9 bytes depending on value.
VarInt
Bitcoin Core source-code name for CompactSize. Different from Protocol Buffers' VarInt (those are continuation-bit-based).
Prefix Byte
First byte. 0x00–0xFC = the value; 0xFD = uint16 follows; 0xFE = uint32 follows; 0xFF = uint64 follows.
Payload
The 2/4/8 little-endian bytes following a 0xFD/0xFE/0xFF prefix. Zero bytes for tier 1.
Tier 1
Single-byte values 0–252. Most common case in real Bitcoin data.
Tier 4
9-byte form (0xFF + uint64). Required by spec but never observed in real Bitcoin transactions.