Talk:Variable-length quantity

Defined for the standard MIDI file format?

 * It was defined for use in the standard MIDI file format[1] to save additional space for a resource constrained system, and ....

Does this sentence mean that no one considered this encoding of integers (in a verifiable way) before the standard MIDI file format was defined in the early 1980s? If so, I am very surprised. --fcp2007 (talk) 21:06, 13 July 2008 (UTC)
 * It more likely means that the MIDI standard was the first to define the name Variable-length Quantity for this concept.


 * Do you know of anyone who encoded quantities in a similar way before the MIDI standard? Please tell us about them. I think this article should mention them, even if they didn't use the exact phrase "variable-length quantity" -- since I agree with the essay WP:REFERS that articles in Wikipedia are about concepts, not particular terms.
 * According to the byte and 8-bit article, 8-bit microprocessors were first introduced in the 1970s. So I would be surprised (but delighted) to see a wp: reliable reference to anyone encoding integers as a variable number of full bytes before the mid 1970s. --DavidCary (talk) 15:51, 8 November 2013 (UTC)

Rename page?
This page should probably be titled "base-128 encoding" or similar, and more generally describe all variations on the theme. (1 or 0 as stop bit, most significant bit group first or last significant, comparison to UTF-8 etc.) —Preceding unsigned comment added by 213.136.42.60 (talk) 13:49, 18 November 2010 (UTC)


 * I think the two pages should be merged, but that the merged article should be titled "Var-Int" because that seems to be more commonly used in the wild than "LEB128". It could also be called "Variable-length integer" with redirections from LEB128, VarInt, VLQ, etc. Also see my comment about other names. That's my two cents. Andyjrobb (talk) 01:36, 20 June 2019 (UTC)

Encoding length with VLQ
Another option is to encode the length n with VLQ and then interpret the following n bytes as a number.

Space usage:
 * VLQ: $$\lceil\log_{128}n\rceil$$ bytes
 * VLQ-length: $$\lceil\log_{128}\lceil\log_{256}n\rceil\rceil + \lceil\log_{256}n\rceil$$ bytes
 * VLQ-length-length: $$\lceil\log_{128}\lceil\log_{256}\lceil\log_{256}n\rceil\rceil\rceil + \lceil\log_{256}\lceil\log_{256}n\rceil\rceil + \lceil\log_{256}n\rceil$$ bytes
 * VLQ-length-length-length: $$\lceil\log_{128}\lceil\log_{256}\lceil\log_{256}\lceil\log_{256}n\rceil\rceil\rceil\rceil + \lceil\log_{256}\lceil\log_{256}\lceil\log_{256}n\rceil\rceil\rceil + \lceil\log_{256}\lceil\log_{256}n\rceil\rceil + \lceil\log_{256}n\rceil$$ bytes

Solomon Ucko (talk) 21:59, 4 February 2019 (UTC)

Other Names for this idea
There are countless examples of this format throughout various projects, which should be added to the main article, and related articles linking back somehow:


 * W3C Efficient XML Interchange (EXI) Binary Encoding for Unsigned Integer
 * Google Protocol Buffers Base 128 Varints
 * WebAssembly LEB128
 * Bitcoin?
 * Sqlite?
 * Android Dalvik Java VM Byte Code
 * DWARF2 LEB128 also here LEB128

There is currently a template-tag to move LEB128 to this page, but I think this page should be moved to LEB128. Andyjrobb (talk) 22:35, 19 June 2019 (UTC)

Typo in "Two's complement section?
''LEB128 uses two's complement to represent signed numbers. In this scheme of representation, n bits encodes a range from -2n to 2n-1''. I guess it should be ''-2n - 1 to 2n - 1-1. 178.64.56.11 (talk) 01:20, 24 September 2021 (UTC)

Errors within examples
I think that the table (Variable-length quantity) needs a review. There appear to be many errors in it (such as 7 digit bytes in the row for 134217728, and a "4" in the varint binary representation of 8192).

I've written a script from first principles to generate what I believe to be the correct values, please see the output below.

JSFiddle with the source that generated it: https://jsfiddle.net/w6jL5qy3/

Additionally, I don't see the value in including the two Hexadecimal columns - its just confusing and detracts from the example. - Chieftain Alex 20:15, 18 April 2023 (UTC)