SQLite4
Variable-Length Integers
Not logged in

A variable length integer is an encoding of 64-bit unsigned integers into between 1 and 9 bytes. The encoding has the following properties:

  1. Smaller (and more common) values use fewer bytes and take up less space than larger (and less common) values.
  1. The length of any varint can be determined by looking at just the first byte of the encoding.
  1. Lexicographical and numeric ordering for varints are the same. Hence if a group of varints are order lexicographically (that is to say, if they are order by memcmp() with shorter varints coming first) then those varints will also be in numeric order. This property means that varints can be used as keys in the key/value backend storage and the records will occur in numerical order of the keys.

The encoding is described by algorithms to decode (convert from varint to 8-byte unsigned integer) and to encode (convert from 8-byte unsigned integer to varint). Treat each byte of the encoding as an unsigned integer between 0 and 255. Let the bytes of the encoding be called A0, A1, A2, ..., A8.

Decode

Encode

Let the input value be V.

Other information

BytesMax ValueDigits
1 240 2.3
2 2287 3.3
3 67823 4.8
4 224-1 7.2
5 232-1 9.6
6 240-1 12.0
7 248-1 14.4
8 256-1 16.8
9 264-1 19.2