Home

varints

Varints, or variable-length integers, are a family of binary encodings in which an integer is represented using a varying number of bytes depending on its magnitude. A common form is base-128 varints, often called LEB128-style varints, where each byte carries seven data bits and a continuation bit in the most significant bit. If the continuation bit is set, more bytes follow; if it is clear, that byte completes the value.

Decoding a varint involves reading bytes in sequence, collecting the seven data bits from each byte, and

Varints are widely used in binary serialization formats because they save space when values are small and

For signed integers, variants exist. Some systems store signed values as unsigned varints after applying a

Limitations include potential variance in maximum length (e.g., up to ten bytes for 64-bit values) and the

placing
them
at
successive
positions
in
the
final
value.
The
process
ends
when
a
byte
with
the
continuation
bit
zero
is
read.
This
design
makes
small
integers
occupy
only
one
byte,
while
larger
values
use
more
bytes.
For
a
64-bit
integer,
a
typical
maximum
of
ten
bytes
is
required.
offer
fast
parsing.
They
appear
in
protocols
and
data
formats
such
as
Protocol
Buffers,
Apache
Thrift,
Cap'n
Proto,
Parquet,
and
several
custom
binary
schemas.
They
are
especially
efficient
when
the
data
set
contains
many
small
values.
ZigZag
encoding,
which
maps
small
negative
and
positive
numbers
to
small
unsigned
values.
Others
use
direct
signed
encodings
such
as
SLEB128,
which
encodes
signed
values
in
a
way
similar
to
unsigned
varints
but
with
sign
information
preserved.
need
for
consistent
decoding
rules.
Varints
are
not
inherently
endian-dependent,
but
correct
handling
of
continuation
bits
is
essential
for
interoperability.