Skip to content
Varint Encoding
step 1/5

Reading — step 1 of 5

Read

~1 min readSchema & Encoding

Varint Encoding

Protobuf encodes integers as varints: variable-length, smaller for smaller values.

Each byte: low 7 bits = data, high bit = continuation. Little-endian within data.

1     -> 00000001                            (1 byte)
127   -> 01111111                            (1 byte, max 1-byte)
128   -> 10000000 00000001                   (2 bytes)
255   -> 11111111 00000001                   (2 bytes)
16384 -> 10000000 10000000 00000001          (3 bytes)

Encoding:

python

Decoding:

python

Common values (small ints) take 1-2 bytes. Big ints (max 10 bytes for 64-bit).

Negative numbers (sint32, sint64):

  • ZigZag encoding: maps signed to unsigned with small magnitudes.
  • ZigZag(n) = (n << 1) ^ (n >> 31) # for 32-bit
  • 0 → 0, -1 → 1, 1 → 2, -2 → 3, 2 → 4, ...
  • Then varint-encode the result.
  • Without zigzag: -1 = 0xFFFFFFFF... = 10 bytes!

Fixed-width:

  • fixed32, fixed64: always 4/8 bytes. Skip varint cost.
  • sfixed32, sfixed64: signed.
  • For values typically large.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…