ArticleslgStudy

computer science

Variable-length integer

Variable-length integer is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Variable-length integer rather than just read about it. In short: A variable-length integer is a variable-length encoded representation of an integer value using a sequence of bytes. The number of bytes that is needed depends on the value being represented, with larger magnitudes using more bytes than smaller ones.

Variable-length integer — main illustration
Variable-length integer — illustration

Key takeaways

  • Variable-length integer belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Variable-length integer to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Variable-length integer from memory before moving on to harder problems.

Reference excerpt

A variable-length integer is a variable-length encoded representation of an integer value using a sequence of bytes. The number of bytes that is needed depends on the value being represented, with larger magnitudes using more bytes than smaller ones. A variable-length integer has no inherent bound on the range of values that can be expressed, unless such a bound is imposed in a particular application. Such schemes have been in use in applications at least since the 1983 specification of the MIDI file format. Variations exist for representing signed and unsigned integers. Variable-length integers are used in applications where it is important to be able to support a large range of integer values and in which smaller numbers are more common than larger ones. Several variations of such formats exist. Early versions used in the MIDI file format and Abstract Syntax Notation One (ASN.1) used big-endian representation. A later-developed little-endian variation is called LEB128. Most variable-length integer representation methods have some built-in redundancy, such that values can be represented using more bytes than necessary, unless the longer representations are prohibited in a particular application (e.g., by prohibiting certain leading or trailing byte values).

Applications and history Various names exist for formats encoding variable-length integers, such as Varint, VInt, VB (Variable Byte), VByte, EncInt, etc. A variable-length quantity (VLQ) was defined for use in the standard MIDI file format, version 1.0 of which was published in 1983. It is also used in the later Extensible Music Format (XMF), first published in 2001. Base-128 is also used in the basic encoding rules of Abstract Syntax Notation One (ASN.1) to encode tag numbers and object identifiers (first published in 1984). It is also used in the (now-obsolete) WAP environment introduced in 1999, where it is called variable length unsigned integer or uintvar. RFC 6256, issued in May 2011, defines the same format and refers to it as a self-delimiting numeric value (SDNV) for use in delay-tolerant networking protocols. The DWARF debugging format defines a variant called LEB128 (or ULEB128 for unsigned numbers), where the least significant group of 7 bits is encoded in the first byte, and the most significant bits are in the last byte (so it is effectively the little-endian analog of a VLQ). Google Protocol Buffers use a similar format to have compact representation of integer values, as does Oracle Portable Object Format (POF) and the Microsoft .NET Framework "7-bit encoded int" in the BinaryReader and BinaryWriter classes. It is also used extensively in web browsers for source mapping – which contain a lot of integer line and column number mappings – to keep the size of the map to a minimum. Variable-width integers in LLVM use a similar principle. The encoding chunks are little-endian and need not be 8 bits in size. The LLVM documentation describes a field that uses 4-bit chunk, with each chunk consisting of 1 bit continuation and 3 bits payload.

Benefits The primary benefits of variable-length integer encoding are its compactness and its flexibility to support any range of values. In most applications, smaller integers are more commonly encountered than very large ones. With variable-length integer encoding, small numbers use fewer bytes, which typically results in using fewer bytes on average if a large quantity of integer values are encoded this way. At the same time, variable-length integer encoding can also represent arbitrarily large integers, unlike fixed-wordlength representations such as 8-bit bytes, 16-bit words, 32-bit integers, or 64-bit long integers. There is no inherent upper bound on the range of values that can be represented by a variable-length integer encoding. In contrast, fixed-length integer representations would typically have either an inadequate range of representable values if a short wordlength is used, or can use too much data to represent each integer if a longer wordlength is used. Variable-length integer encoding is also simpler to encode and decode than most other variable-length codes such as Huffman codes, since VLQ codes use whole-byte chunks of data. More general variable-length codes typically require much more bit-oriented manipulation for encoding and decoding.

General structure The encoding assumes an octet (an 8-bit byte) where the most significant bit (MSB), also commonly known as the sign bit, is reserved to indicate whether another VLQ octet follows.

If A is 0, then this is the last VLQ octet of the integer. If A is 1, then another VLQ octet follows. B is a 7-bit number [0x00, 0x7F], and n is the position of the VLQ octet where B0 is the least significant. The VLQ octets are arranged most significant first in a stream.

Variants The general VLQ encoding is simple, but in basic form is only defined for unsigned integers (nonnegative, positive or zero), and is somewhat redundant, since prepending 0x80 octets corresponds to zero padding. There are various signed number representations to handle negative numbers, and techniques to remove the redundancy.

Group Varint Encoding Google developed Group Varint Encoding (GVE) after observing that traditional VLQ encoding incurs many CPU branches during decompression. GVE uses a single byte as a header for 4 variable-length uint32 values. The header byte has 4 2-bit numbers representing the storage length of each of the following 4 uint32s. Such a layout eliminates the need to check and remove VLQ continuation bits. Data bytes can be copied directly to their destination. This layout reduces CPU branches, making GVE faster than VLQ on modern pipelined CPUs. PrefixVarint is a similar design but with a uint64 maximum. It is said to have "been invented multiple times independently". It is possible to be changed into a chained version with infinitely many continuations.

Signed numbers

Sign bit Negative numbers can be handled using a sign bit, which only needs to be present in the first octet. In the data format for Unreal Packages used by the Unreal Engine, a variable-length quantity scheme called Compact Indices is used. The only difference in this encoding is that the first VLQ octet has the seventh bit reserved to indicate whether the encoded integer is positive or negative. Any consecutive VLQ octet follows the general structure.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Variable-length integer

Start with the simplest possible case. Write down what Variable-length integer claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Variable-length integer before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Variable-length integer ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Variable-length integer

In research
Variable-length integer appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Variable-length integer in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Variable-length integer is common in secondary-school and first-year university syllabi. It links to neighbouring topics Data types, Lossless compression algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Variable-length integer outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Variable-length integer” →

How to study Variable-length integer in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Variable-length integer means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Variable-length integer out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Variable-length integer in simple terms?

A variable-length integer is a variable-length encoded representation of an integer value using a sequence of bytes. The number of bytes that is needed depends on the value being represented, with larger magnitudes using more bytes than smaller ones.

Why does Variable-length integer matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Variable-length integer?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Variable-length integer.

Tags

  • Data types
  • Lossless compression algorithms

Keep exploring