ArticleslgStudy

computer science

LEB128

LEB128 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 LEB128 rather than just read about it. In short: LEB128 or Little Endian Base 128 is a variable-length code compression used to store arbitrarily large integers in a small number of bytes. LEB128 is used in the DWARF debug file format and the WebAssembly binary encoding for all integer literals.

Key takeaways

  • LEB128 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 LEB128 to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of LEB128 from memory before moving on to harder problems.

Reference excerpt

LEB128 or Little Endian Base 128 is a variable-length code compression used to store arbitrarily large integers in a small number of bytes. LEB128 is used in the DWARF debug file format and the WebAssembly binary encoding for all integer literals.

Encoding format LEB128 format is very similar to the variable-length quantity (VLQ) format used in MIDI and ASN.1; the primary difference is that LEB128 is little-endian whereas variable-length quantities are big-endian. Both allow small numbers to be stored in a single byte, while also allowing encoding of arbitrarily long numbers. There are two versions of LEB128: unsigned LEB128 and signed LEB128. The decoder must know whether the encoded value is unsigned LEB128 or signed LEB128.

Unsigned LEB128 To encode an unsigned number using unsigned LEB128 (ULEB128) first represent the number in binary. Then zero extend the number up to a multiple of seven bits (such that if the number is non-zero, the most significant seven bits are not all 0). Break the number up into groups of seven bits. Output one encoded byte for each seven bit group, from least significant to most significant group. Each byte will have the group in its seven least significant bits. Set the most significant bit on each byte except the last byte. The number zero is usually encoded as a single byte 0x00. WebAssembly allows alternate encodings of zero (0x80 0x00, 0x80 0x80 0x00, ...). As an example, here is how the unsigned number 624485 gets encoded:

MSB ------------------ LSB 10011000011101100101 In raw binary 010011000011101100101 Padded to a multiple of 7 bits 0100110 0001110 1100101 Split into 7-bit groups 00100110 10001110 11100101 Add high 1 bits on all but last (most significant) group to form bytes 0x26 0x8E 0xE5 In hexadecimal

→ 0xE5 0x8E 0x26 Output stream (LSB to MSB)

Unsigned LEB128 and VLQ (variable-length quantity) both compress any given integer into not only the same number of bits, but exactly the same bits—the two formats differ only in exactly how those bits are arranged.

Signed LEB128 A signed number is represented similarly: Starting with an N-bit two's complement representation, where N is a multiple of 7, the number is broken into groups as for the unsigned encoding. For example, the signed number -123456 is encoded as 0xC0 0xBB 0x78:

MSB ------------------ LSB 11110001001000000 Binary encoding of 123456 (begin with unsigned) 000011110001001000000 As a 21-bit number 111100001110110111111 Negating all bits (ones' complement) 111100001110111000000 Adding one (two's complement) 1111000 0111011 1000000 Split into 7-bit groups 01111000 10111011 11000000 Add high 1 bits on all but last (most significant) group to form bytes 0x78 0xBB 0xC0 In hexadecimal

→ 0xC0 0xBB 0x78 Output stream (LSB to MSB)

Fast decoding A straightforward scalar implementation of LEB128 decoding is fairly slow, even more so on modern hardware where branch misprediction is relatively expensive. A series of papers presents SIMD techniques for accelerating decoding (it is called VByte in these papers, but is another name for the same encoding). The "Vectorized VByte Decoding" paper presented "Masked VByte", which demonstrated speeds of 650–2700 million integers per second on commodity Haswell hardware, depending on encoding density. A followup paper presented a variant encoding, "Stream VByte: Faster Byte Oriented Integer Compression", which increased speeds to over 4 billion integers per second. This stream encoding separates the control stream from the encoded data, so is not binary compatible with LEB128.

C-like pseudocode

Encode unsigned integer

Encode signed integer

Decode unsigned integer

Decode signed integer

JavaScript code

Encode signed BigInt

Decode signed BigInt

Uses The Android project uses LEB128 in its Dalvik Executable Format (.dex) file format. Compressing tables in Hewlett-Packard IA-64 exception handling. The DWARF file format uses both unsigned and signed LEB128 encoding for various fields. LLVM, in its Coverage Mapping Format LLVM's implementation of LEB128 encoding and decoding is useful alongside the pseudocode above. .NET supports a "7-bit encoded int" format in the BinaryReader and BinaryWriter classes. When writing a string to a BinaryWriter, the string length is encoded with this method. Minecraft uses LEB128 in its protocol for measuring lengths of data within packets. The mpatrol debugging tool uses LEB128 in its tracing file format. osu! uses LEB128 in its osu! replay (.osr) format. W3C Efficient XML Interchange (EXI) represents unsigned integers using LEB128, in exactly the same way as described here. WebAssembly, in its portable binary encoding of the modules In the xz file format

Related encodings Dlugosz' Variable-Length Integer Encoding (original) uses multiples of 7 bits for the first three size breaks, but after that the increments vary. It also puts all the prefix bits at the beginning of the word, instead of at the beginning of each byte. Human interface device report descriptor bytes use a byte-count bitfield of 2 bits to encode the size of the following integer of zero, one, two, or four bytes, always little endian. Signedness, i.e. whether to expand the shortened integer with sign or not, depends on the descriptor type. The LLVM bitcode file format uses a similar technique except that the value is broken into groups of bits of context-dependent size, with the highest bit indicating a continuation, instead of a fixed 7 bits. Protocol Buffers (Protobuf) uses the same encoding for unsigned integers, but encode signed integers by prepending the sign as the least significant bit of the first byte. ASN.1 BER, DER Encode values of each ASN.1 type as a string of eight-bit octets

References

See also The DWARF debugging file format UTF-7

Worked examples

Example 1 — a first encounter with LEB128

Start with the simplest possible case. Write down what LEB128 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 LEB128 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 LEB128 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 LEB128

In research
LEB128 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 LEB128 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
LEB128 is common in secondary-school and first-year university syllabi. It links to neighbouring topics Lossless compression algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for LEB128 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 “LEB128” →

How to study LEB128 in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what LEB128 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 LEB128 out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is LEB128 in simple terms?

LEB128 or Little Endian Base 128 is a variable-length code compression used to store arbitrarily large integers in a small number of bytes. LEB128 is used in the DWARF debug file format and the WebAssembly binary encoding for all integer literals.

Why does LEB128 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 LEB128?

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 LEB128.

Tags

  • Lossless compression algorithms

Keep exploring