Varint
A Varint (variable-length integer) is the variable-length integer encoding used by Protobuf and other protocols: each byte carries 7 bits of payload in its low bits, and the most significant bit is the continuation flag.
Purpose
It represents integers compactly in serialization protocols — small integers take very little space, and large ones extend automatically.
Mechanism
The unsigned integer is split into 7-bit groups emitted from least to most significant; a 1 in a byte's MSB means more bytes follow, and a 0 marks the end. Signed integers are first mapped to unsigned via ZigZag.
Use cases
- Protobuf field encoding
- Variable-length database columns
- Compact binary protocols