← All tools

Move-to-Front Transform

The Move-to-Front (MTF) transform rewrites a string as a sequence of small integers by exploiting locality: keep a list of all 256 byte values, and for each input byte output its current position in the list, then move that byte to the front. Repeated characters in a row become runs of zeros, and recently-seen bytes get small codes — so MTF is the standard second stage after the Burrows–Wheeler transform in bzip2, turning BWT's clustered runs into a stream dominated by zeros that entropy coding (RLE / Huffman) then crushes. MTF is lossless and exactly invertible. See the forward codes, the inverse round-trip, and a step-by-step trace of the moving front of the symbol table. Everything runs locally in your browser.

Input text

Forward transform

Step trace (first 24 symbols)

Inverse / round-trip

How it works. Initialise the symbol list as [0, 1, 2, …, 255]. Forward: for each input byte b, emit its index in the list, then move b to position 0 (remove it, insert at front). Inverse: for each index i, output the byte currently at position i, then move that byte to the front. Both share the same list update rule, so the inverse rebuilds the exact original — inverse(forward(x)) = x always. Two key properties: the first code equals the first byte's value (the list starts as the identity); and any immediately-repeated byte codes as 0, since after one move-to-front it sits at position 0. That is why MTF shines on BWT output, which is full of such runs. Pairs with the Burrows–Wheeler Transform, Run-Length Encoding, Huffman Coding, and LZW tools. Everything runs locally — nothing leaves your browser.