varint
"varint", or long form variable-length integer, is a way to encode arbitrarily-large integer. It's used in a lot of places in many different variants, but the main idea is as follows:
- A varint is composed of multiple octets;
- The most-significant bit of every octet is the continuation bit; within an octet stream, if this bit is set to 1, then the next octet should still be considered as a part of the current varint; if this bit is set to 0, then this very octet should be considered to be the last octet of this varint.
- The actual value of the integer shall be composed by the other 7 bits of the octets, either in a little-endian or a big-endian manner.
For example, when seen as the most basic form of varint described above, the octet sequence 0xfb 0x67 is read as follows:
- The first octet,
0xfb, is1111 1011in binary, within which the continuation bit (i.e. most-significant bit) is1. The other 7 bits is111 1011. - Since the continuation bit is
1, we need to read the next octet. - The next octet,
0x67, is0100 0011in binary. Its continuation bit is0, thus we will not consider the next octet to be a part of this varint. The other 7 bits of this octet is100 0011. - Now, depending on whether the specification calls for little-endian or big-endian, the result could be
111 1011 100 0011which is0x3dc3(big-endian) or100 0011 111 1011which is0x21fb(little-endian).
Big-endian varint can be seen in WBMP. Little-endian varint can be seen in Git, which curiously uses 3 different variants.
2025.4.15