Skip to content

Protobuf's encoding uses a few bit-level operations: byte order, bit shifting, and the bitwise operators OR, AND, and XOR. If you can already read x << 3 | y, skip ahead. If not, here's the short version.

A byte is eight bits. This chapter writes each byte as two hex digits, so 0a is the bits 00001010, and the lowest bit is the one on the right.

Endianness

Multi-byte numbers can be laid out least significant byte first (little-endian) or most significant byte first (big-endian). Protobuf's fixed-width types (fixed32, fixed64, float, double) are little-endian. That's why the low bytes of a number show up first in the raw stream.

00000011111010001110100000000011

Bit Shifting

A left shift (<<) moves every bit left and fills the gap with zeroes. Shifting left by 3 multiplies by 8 and leaves 3 empty bits on the right. Protobuf uses this to build the field tag. It shifts the field number left by 3 to make room for the wire type.

0000000100000001000

Bitwise OR

OR (|) merges two numbers. An output bit is 1 if either input bit is 1. After a shift clears space, OR writes another value into it. As long as the two values occupy non-overlapping bit ranges, neither one corrupts the other.

000110000000101000011010

Right Shift, AND, and XOR

Decoding runs those steps backwards. A right shift (>>) moves every bit right and drops the ones that fall off the end, so tag >> 3 is the field number. AND (&) keeps a bit only where both inputs have a 1, so tag & 7 keeps the low three bits, the wire type. XOR (^) flips a bit wherever the other input has a 1. ZigZag uses it to turn small negative numbers into small positive ones.

Further Reading

  • Endianness

    Little- and big-endian byte order, and why it matters when bytes move between machines.

  • Bit shifts

    The shift operations behind the tag formula, including the arithmetic/logical distinction this page glosses over.

  • Bitwise OR

    How OR merges bit patterns, along with the AND and XOR operations decoders also use.

Next

Base-128 Varints

How Protobuf base-128 varints encode integers in as few bytes as possible: continuation bits, byte layout, and an interactive varint encoder.