wire / wire-format / leb128-varint

LEB128 / protobuf varint (little-endian base-128)

A varint encodes an unsigned integer in a variable number of bytes, 7 bits of payload per byte, little-endian (least-significant group first). The top bit (0x80) of each byte is a continuation flag: 1 means more bytes follow, 0 means this is the last. Used by Protocol Buffers, DWARF, WebAssembly, and many length prefixes.

wire-format kind wire-format status de-facto verification verified tier C wire-format@1

aka: varint · LEB128 · ULEB128 · SLEB128 · base-128 varint

wire format

spec: Protocol Buffers / DWARF LEB128

Split the integer into 7-bit groups, least-significant first. Emit each group in a byte; set the high bit (0x80) on every byte except the last to mark continuation. Decoding accumulates 7 bits at a time, shifting left by 7 per byte, until a byte with the high bit clear. SLEB128 (signed) sign-extends from the final group's high payload bit instead of zero-padding.

fieldsizemeaning
Group byte (repeated)1 byte eachBit 8 (0x80) = continuation flag (1 = more bytes follow, 0 = final byte). Bits 7-1 = the next 7 bits of the value, least-significant group first.

example:

96 01

150. Byte 0x96 = 1001_0110: continuation bit set, payload 0x16 = 22 (low 7 bits). Byte 0x01 = 0000_0001: final, payload 1 => high 7 bits. Value = (1 << 7) | 22 = 128 + 22 = 150.

see: wire-format/protobuf-wire


provenance

per-fact attribution:

see also

agent: curl -H 'accept: application/json' wire.phall.io/wire-format/leb128-varint or /wire-format/leb128-varint.json