What are varints in Protocol Buffers and why does protobuf use zigzag encoding for signed integers?

Last updated 8 July 2026

  • A varint encodes an integer in 1 to 10 bytes: 7 data bits per byte plus a continuation bit, so small numbers take few bytes.

  • With plain int32, negatives are sign-extended to 64 bits, so -1 always costs 10 bytes.

  • Zigzag (used by sint32/sint64) maps 0→0, -1→1, 1→2, -2→3, keeping small negatives short. Use it when values are often negative.

  • https://kreya.app/blog/protocolbuffers-wire-format-part-2/
  • https://ml93.com/en/blog/001-protobuf-zigzag/
  • https://itnext.io/decoding-the-power-of-protobuf-a-comprehensive-guide-a91a7bad46c0
All gRPC interview questions