RC RANDOM CHAOS

Squeezing Every Country's Flag Into ~11 Bits With Huffman Trees

· via Hacker News

Original source

Compressing a flag to 11 bits

Hacker News →

Inspired by a math video that used flags to illustrate matrix operations, a developer set out to see how compactly a country’s flag could be encoded. The premise: most flags are visually simple—stripes, a star or crescent, a colored corner—so a description like ‘three vertical stripes: blue, white, red’ should need only a handful of bits given a matching decoder and renderer. The goal was recognizability rather than fidelity, so exact shades, precise placements, and fine detail were all treated as expendable. Coat-of-arms flags (Andorra), non-rectangular ones (Nepal), and anything beyond national flags were excluded because they’d force an embedded bitmap or vector and defeat the whole point.

The design decomposes a flag into a small set of parameters—aspect ratio, a palette of general color groups rather than exact hex codes, and a stack of Photoshop-style layers. A ‘Stripe’ layer handles count, colors, orientation, and spacing; additional ‘Shapes,’ ‘Band,’ and ‘Region’ layers place stars, crescents, and colored rectangles. The US flag, for instance, becomes a stripe layer, a blue rectangle region, then a star layer.

Because flag attributes follow Zipf’s law—a few aspect ratios, colors, and layouts dominate while a long tail of oddities remains—the author encodes each attribute with its own variable-length Huffman tree, giving the common cases very short codes (2:3 aspect ratio is a single bit). To avoid bloating the trees with one-off values, each tree ends in a ‘Custom’ escape leaf followed by a fixed-length literal, letting outliers like El Salvador’s 189:335 ratio be represented without polluting the shared encoding. The result is a neat demonstration of how domain structure plus entropy coding can shrink seemingly arbitrary data down to a few bits.

Read the full article

Continue reading at Hacker News →

This is an AI-generated summary. Read the original for the full story.