A raw Bitcoin transaction is a sequence of bytes, not a spreadsheet with a fixed column for every possible input. Software needs to know where one list ends and the next field begins. Small counters and length prefixes supply that structure.

One commonly used encoding is called CompactSize. Its purpose is modest but essential: represent an unsigned integer compactly when the number is small, while leaving room for larger numbers. Understanding it helps explain why a transaction parser cannot simply jump a fixed number of characters after the version field and expect to find the outputs.

A count is not the thing being counted

Bitcoin's transaction reference identifies the input count and output count as CompactSize integers. The counts tell a reader how many input or output structures follow. Within those structures, length fields identify the number of bytes occupied by scripts. Transaction format reference

Those are different jobs. A count of two outputs means two output structures, not two bytes of output data. A script length of 22 means 22 bytes of script, not 22 outputs. The enclosing format tells the parser which meaning applies.

This distinction is useful when comparing a wallet's decoded display with raw hexadecimal data. A friendly screen may show a neat list of recipients. The encoded transaction instead supplies fields in a defined sequence, with enough structure for software to locate each piece without those display labels.

Why 253 needs more room than 252

CompactSize uses one byte for values below 253. The next three possible first-byte values act as markers: 253 introduces a two-byte integer, 254 introduces a four-byte integer, and 255 introduces an eight-byte integer. Including the marker, those encodings occupy three, five and nine bytes. CompactSize encoding reference

For example, the decimal number 252 can be encoded as hexadecimal fc. The number 253 is encoded as fd fd 00: a marker followed by the two-byte value in little-endian order. This is an encoding example, not a sample payment or a claim about a typical wallet's input count.

The important change at that boundary is that the counter itself grows by two bytes. If another input is also being added, its complete structure consumes additional space. Confusing those two changes leads to wrong size estimates: the cost of a larger list is not just the difference between its counters.

Witness data has structure too

Segregated Witness adds another place where counting matters. BIP144 describes a witness as a stack of byte arrays. There is an item count, followed by a length and the bytes for each item. Each input has an associated witness structure; their order follows the inputs. BIP144 serialization

That means a reader must track both how many items remain and how many bytes belong to the current item. A signature-like byte string does not become a signature merely because a person recognizes its shape in a hex dump. Its position and the applicable spending rules determine how it is interpreted.

BIP141 separately explains that a witness is not itself Bitcoin script, even though witness data participates in validation. That distinction is another reason to use the appropriate parser rather than treat every byte region as a list of script instructions. BIP141 witness structure

Different encodings are not interchangeable

Bitcoin Core's serialization code also contains a separate variable-length integer mechanism. The fact that two routines both encode numbers using variable space does not make their byte representations interchangeable. The code additionally rejects noncanonical CompactSize forms, such as using the longer marked form for a number that belongs in the shorter form. Serialization checks

For application developers, our recommendation is simple: select the encoding required by the specific format, not a library function whose name merely sounds close. Test boundary values and truncated input as well as the easy one-byte case. A decoder that handles one ordinary payment correctly has not demonstrated that it handles all permitted lengths safely.

For everyone else, this is not a reason to edit transactions by hand. It is a reason to distrust explanations that treat raw data as a collection of freely replaceable text fields. Changing a length without changing the following data coherently can make the remainder unreadable.

CompactSize is bookkeeping, not a new spending feature or a compression service that wallets can switch on for extra savings. Its contribution is predictable structure. It lets many everyday counts remain small while giving a decoder an explicit way to recognize larger ones. Those unobtrusive boundaries are part of what turns a long hexadecimal string into a transaction software can actually interpret.