The guide
Folding a line into a square: why the Hilbert curve keeps neighbours close, and where it still fails
The Hilbert curve visits every cell of a grid without one jump, and any aligned run of cells fills a square. Close along the line means close on the plane, but the reverse does not always hold.
- 63/8 length of the order-3 curve, which takes 63 steps
- 2,047 of 4,095 Morton steps that jump at order 6; Hilbert order has none
- 50.8% touching cell pairs that are also neighbours on the curve, order 6
- 3,413 largest index gap between two touching cells on the order-6 curve
- 16 by 16 square filled by 256 consecutive cells on an order-12 grid
Read the full guide Fold the guide
Walk 37 steps along the Hilbert curve on an 8 by 8 grid and you stand on the cell in column 4, row 7. Write 37 in base 4 and you get 211. Each digit names a quarter: the first picks one of four quarters of the whole grid, the second picks a quarter of that, and the last picks a single cell.
That is the whole idea of the curve. A single number, the distance d, stands for a place on a plane, and the place can be recovered from the number with a handful of whole-number operations. Nothing else is stored. The curve was described by David Hilbert in 1891 as a variant of the space-filling curves that Giuseppe Peano had found in 1890. Wikipedia says its image is the unit square, so its Hausdorff dimension is 2. A line that fills an area is a strange object, and the strangeness is the reason it is useful.
Folding a line into a square
The curve on the page is not the true curve. It is one of the piecewise linear curves whose limit is the true one. Wikipedia gives the length of the n-th curve as 2^n minus 1/2^n, so the length grows exponentially even though every curve fits in a square of area 1.
Count it directly. The order-n curve visits 4^n cells and joins them with 4^n minus 1 steps, each of length 1/2^n. At order 3 that is 63 steps of 1/8, a length of 63/8, or 7.875. At order 5 it is 1,023 steps of 1/32, a length of 1,023/32, or 31.96875. The usual slip is to count 4^n steps. That gives a length of exactly 2^n, which is 8 at order 3. A line that stops one step short of closing its count is a fence-post error, and the page shows both numbers so the difference is visible.
How do you build the curve of order n? Take four copies of the curve of order n minus 1, one in each quarter of the square. Reflect the first across the main diagonal and the last across the other diagonal, and leave the two between them as they are. The end of each copy then sits next to the start of the next.
That reflection is the step that people forget. Take the digit of each pair of bits, place the quarters in the right positions, and skip the turning, and every cell is still visited exactly once. But the path jumps between quarters. At order 3 the curve built without the turning breaks the neighbour rule 10 times in 63 steps.
Bit interleaving alone is something else again. Wikipedia describes Z-order, also called Morton order, as the order you get by interleaving the bits of the coordinates, and notes that it is named after Henri Lebesgue, who studied it in 1904, and Guy Morton, who applied it to file sequencing in 1966. It is a perfectly good order. It is not the Hilbert curve. At order 3 it jumps 31 times in 63 steps.
Half the steps of Morton order are jumps. At order 6, 2,047 of 4,095 steps leave the neighbourhood of the cell they started from, while the Hilbert curve has none. The tool counts both and draws the Morton jumps as dashed lines.
Why a run of cells fills a square
Here is the property that makes the curve practical. Cut the index range into runs of 4^k consecutive numbers, starting at multiples of 4^k. Every such run fills one aligned square of 2^k by 2^k cells, with nothing left over and nothing outside. Take d = 37 at order 3. It lies in the run of 16 cells from index 32 to index 47, and those 16 cells fill the 4 by 4 square whose corner is column 4, row 4. The last digit of d moves you inside that square. The first digits chose the square itself.
The word aligned matters. Start the run at index 1 instead of index 0 and the 16 cells span a box 4 columns wide and 5 rows tall, which is no square. Change the start by one and the clean shape is gone. The tool shows the aligned run and says so. Network addresses are the best-known use. IPv4 has 2^32 addresses, and Wikipedia’s page on classless routing says a prefix of length L covers 2^(32 minus L) of them. Give each cell 256 addresses and the whole space fits an order-12 grid of 16,777,216 cells.
On that grid a /24 block is one cell, a /16 block is 256 consecutive cells that fill a 16 by 16 square, and a /8 block is 65,536 cells that fill a 256 by 256 square. The Hilbert page says the curve is used to map address ranges into a picture because it keeps nearby addresses close together. In the tool, the address 10.20.30.40 lands in cell (868, 770) at distance 660,510, and its /16 block is the 16 by 16 square from (864, 768).
Close on the line, close on the plane
Wikipedia states the locality property plainly. Both the true curve and its discrete approximations map between one and two dimensions in a way that preserves locality fairly well. Two points that are close in one dimension are close after folding. The converse, it adds, is not always true.
Put numbers on it. At order 6 the grid has 8,064 pairs of cells that share an edge. On the Hilbert curve 4,095 of those pairs, 50.8%, are also neighbours along the curve. On the Morton curve only 2,048, or 25.4%, are. So Hilbert order wins, and the story could stop there. It does not. The largest index gap between two touching cells is 3,413 for the Hilbert curve and 1,366 for Morton order. The mean gap is 39.1 for Hilbert and 32.5 for Morton.
Read that again. Hilbert order has no jumps and twice the adjacency, yet on the worst pair and on the average pair Morton order sits closer. The worst pair sits on the seam between the first and last quarters of the curve. The cells (31, 0) and (32, 0) touch, yet they are distance 341 and distance 3,754 along the curve, 3,413 steps apart, about five sixths of its whole length. No single measure favours either order, and the tool prints all four.
I computed these figures with whole numbers in Python 3.13, using fractions where a ratio was needed, and again with a second routine that builds the curve by quarters rather than by bits. The page repeats the same comparison in a background worker every time you open it.
What it means for an index
A database that sorts records by a curve index turns a two-dimensional search into a one-dimensional one. Wikipedia explains the usual form for Z-order: interleave the bits of the coordinates, sort by the result, and keep the records in any one-dimensional structure such as a balanced tree. The catch is the range query. A rectangle on the plane is not one interval of the index. Wikipedia describes the problem of finding, from a point met during the search, the next index value that still lies inside the rectangle, and says a solution was shown by Tropf and Herzog in 1981. The converse failure above is why the box scatters.
Hilbert order has been proposed in place of Z-order because its order-preserving behaviour is better. The aligned runs help: a box that matches an aligned square is exactly one run. A box that cuts across the seams is several runs, and the gap between them can be large. Fewer and shorter detours is the practical goal, and no curve removes them.
The curve appears elsewhere for the same reason. Wikipedia lists Riemersma dithering, which carries the leftover of each pixel to the next pixel along the curve, so the picture avoids the distracting patterns that a row-by-row order makes. It also lists exploring regions with mobile robots, indexing geospatial data and an infill pattern in 3D printing slicers.
Reading it from the rules
The curve has a second description that needs no arithmetic at all. Wikipedia gives it as an L-system with the letters A and B, the constants F, plus and minus, the axiom A, and the rules A becomes +BF-AFA-FB+ and B becomes -AF+BFB+FA-. F means draw forward, plus turns left 90 degrees, and minus turns right 90 degrees. A and B are ignored when drawing. That is turtle graphics: a cursor that moves relative to itself, the way Wikipedia describes the turtle of the Logo language.
Expand the rules once and you get 11 characters. Expand again and you get 51, then 211, 851, 3,411 and 13,651 characters at orders 1 to 6. The count follows (10 times 4^n minus 7) divided by 3, and each string holds exactly 4^n minus 1 forward steps, one for every step of the curve.
Draw the string with a turtle that starts heading east on a screen where y grows downward, and it traces the same curve as the converter, mirrored top to bottom. Read it from the other end and it matches the converter turned through a half turn. Wikipedia describes the eight symmetries of a square: four turns and four reflections. The tool names the one it finds.
The arithmetic stays exact because 4^26 minus 1 is below 2^53, the point where ordinary numbers stop holding every whole number. The page therefore allows orders up to 26 and uses exact big integers only for checks, groups its digits with the browser’s own number formatting, and does the heavy drawing work in a worker so that the page stays responsive.
What the tool cannot do
It is a calculator for the discrete curve on a square grid. It does not handle three or more dimensions, the Moore curve or other variants, grids that are not square, or R-tree packing. It does not compute the limit curve; that object is only named here. Locality figures stop at order 8, the L-system at order 6, and the whole curve is drawn only up to order 7.
Larger orders are still exact. They are drawn as a 32 by 32 window around your cell, or as a coarse 128 by 128 picture of blocks. Type order 12 and d = 660,510 and you can follow one address down from the whole map to the cell it lives in.
Sources
Every source consulted for this guide. Links checked November 12, 2026.
- Hilbert curve - Wikipedia Wikipedia (CC BY-SA 4.0)
- Z-order curve - Wikipedia Wikipedia (CC BY-SA 4.0)
- Space-filling curve - Wikipedia Wikipedia (CC BY-SA 4.0)
- Peano curve - Wikipedia Wikipedia (CC BY-SA 4.0)
- L-system - Wikipedia Wikipedia (CC BY-SA 4.0)
- Hausdorff dimension - Wikipedia Wikipedia (CC BY-SA 4.0)
- Dihedral group of order 8 - Wikipedia Wikipedia (CC BY-SA 4.0)
- Classless Inter-Domain Routing - Wikipedia Wikipedia (CC BY-SA 4.0)
- Dither - Wikipedia Wikipedia (CC BY-SA 4.0)
- Turtle graphics - Wikipedia Wikipedia (CC BY-SA 4.0)
- Web Workers API - Web APIs | MDN MDN Web Docs (CC BY-SA 2.5 or later)
- BigInt - JavaScript | MDN MDN Web Docs (CC BY-SA 2.5 or later)
- Intl.NumberFormat - JavaScript | MDN MDN Web Docs (CC BY-SA 2.5 or later)
- fractions - Rational numbers - Python 3.13 documentation Python Software Foundation (PSF License Version 2)