Outputs: Output count

TransactionOutput Count
Structure
Version
4 bytes
Segwit (Optional)
Marker
1 byte
Flag
1 byte
Structure
Input Count
variable
Input Structure
Previous TXID
32 bytes
Output Index
4 bytes
ScriptSig Size
variable
ScriptSig
variable
Sequence
4 bytes
Structure
Output Count
variable
Output Structure
Amount
8 bytes
ScriptPubKey Size
variable
ScriptPubKey
variable
Structure
Witness
variable
Structure
Locktime
4 bytes

Size

variable

Format

Compact Size

Description

Number of outputs in this transaction.

Example

02

Byte Visualization

02
Byte 1

The number of outputs in a transaction is stored as a variable-length integer (varint). Let's examine a real transaction:

SVG Image

The output count tells us how many outputs to expect in this transaction. Like the input count, it uses varint encoding:

Decimal Range

Hex Range

Bytes used

Format

0 to 252

0x00 to 0xfc

1uint8_t
253 to 2¹⁶-1

0xfd to 0xffff

3

0xfd followed by the number as uint16_t

2¹⁶ to 2³²-1

0x10000 to 0xffffffff

5

0xfe followed by the number as uint32_t

2³² to 2⁶⁴-1

0x100000000 to 0xffffffffffffffff

9

0xff followed by the number as uint64_t

Common Output Patterns

Most Bitcoin transactions have either one or two outputs:

  • Single Output: Typically seen in coinbase transactions (mining rewards) or when moving all funds to a new address
  • Two Outputs: The most common pattern, where one output is the payment and the other is change returned to the sender
  • Multiple Outputs: Used for batching multiple payments into a single transaction, which is more efficient

Implementation Example

Here's how you might parse a transaction's output count:

python
def parse_output_count(raw_tx: bytes, offset: int = 0) -> tuple[int, int]:
  """
  Parse output count from raw transaction bytes
  
  Args:
      raw_tx: Raw transaction bytes
      offset: Starting position in bytes
      
  Returns:
      (count, new_offset)
  """
  # Read the varint
  count = 0
  first_byte = raw_tx[offset]
  
  if first_byte < 0xfd:
      count = first_byte
      offset += 1
  elif first_byte == 0xfd:
      count = int.from_bytes(raw_tx[offset+1:offset+3], 'little')
      offset += 3
  elif first_byte == 0xfe:
      count = int.from_bytes(raw_tx[offset+1:offset+5], 'little')
      offset += 5
  else:  # 0xff
      count = int.from_bytes(raw_tx[offset+1:offset+9], 'little')
      offset += 9
      
  return count, offset

Note

Every valid Bitcoin transaction must have at least one output. A transaction with zero outputs would be invalid and rejected by the network.

Suggest Edits