ForHosting KIT · Developer Utilities

Morton Code Decoder

The Morton code decoder reverses a two-dimensional Z-order mapping. Give it one unsigned decimal index and it separates the alternating bits to recover the original x and y coordinates.

● BetaFree · in your browser
Use it from WebAPIEmailTelegramApp soon

It supports the complete 64-bit Morton range, so each decoded coordinate may use all 32 unsigned bits. The operation is exact, deterministic, and useful when inspecting spatial indexes, debugging encoders, reading compact tile identifiers, or verifying database and graphics code without writing a one-off bit manipulation script.

How Morton decoding recovers two coordinates

A two-dimensional Morton code stores two coordinate bit streams inside one integer. Starting at the least significant end, bit position zero belongs to x, bit position one belongs to y, position two returns to x, and the pattern continues. Decoding therefore does not approximate, divide, or infer a location. It extracts every even-positioned bit into the corresponding position of x and every odd-positioned bit into y. For example, a coordinate bit found at Morton position six becomes bit three of x, while a bit at position seven becomes bit three of y. This capability applies that rule across all 64 available code bits, producing two unsigned 32-bit coordinates. The returned code is also normalized as a decimal string, which makes leading-zero inputs unambiguous and preserves the exact original numeric value. This convention is the common two-dimensional layout in which x occupies even bit positions. If an upstream system interleaves y first instead, swap the two returned coordinates or adjust that encoder's convention before comparing results.

Entering large codes without losing precision

Send the Morton index in the code field as an unsigned base-ten integer string, such as "39" or "18446744073709551615". A string is important for large values because ordinary JSON numbers and JavaScript numbers cannot exactly represent every integer across the full unsigned 64-bit range. The decoder parses the string with exact integer arithmetic and never converts the combined Morton code to a floating-point number. Signs, decimal points, exponential notation, whitespace, commas, hexadecimal prefixes, and values above 2^64 minus one are rejected instead of being silently rounded or reinterpreted. Small safe integer values are accepted defensively by the implementation, but the published contract uses a string so the same request remains exact in every SDK and transport. The resulting x and y values never exceed 4,294,967,295, which is within the exact integer range of JSON numbers, so coordinates are returned as convenient numeric fields. A code of zero decodes to x zero and y zero, and the maximum 64-bit code decodes to the maximum value on both axes.

Using the result to test spatial systems

Morton ordering appears in spatial databases, quadtrees, tile stores, voxel and texture layouts, cache-friendly arrays, and graphics pipelines. During debugging, decode a suspicious index and compare the returned point with the coordinate pair that entered your encoder. A correct round trip should reproduce both coordinates exactly under the same x-first bit convention. Boundary tests are especially valuable: try zero, values where only one Morton bit is set, coordinate powers of two, and the maximum supported code. Single-bit cases reveal swapped axes or off-by-one shifts immediately because only one output bit should appear. This decoder deliberately returns coordinates rather than geographic longitude and latitude; Morton coding is a bit layout and does not define a projection, scale, signed-number representation, or coordinate reference system. If your application offsets signed coordinates, quantizes floating-point positions, or projects geographic locations before encoding, reverse those application-specific steps after decoding. The API performs one bounded pass with no network access, randomness, stored state, or hidden platform-dependent integer operations, making it suitable for repeatable tests and automated validation.

Debug a spatial index

Turn an unexpected Z-order key back into x and y to locate an encoder, axis-order, or bit-offset defect.

Inspect quadtree and tile identifiers

Recover the integer grid position represented by a compact Morton key before applying the application's scale or projection.

Verify encoding round trips

Add deterministic decode checks to tests for database, simulation, game, graphics, or cache-local data layouts.

Which bits belong to each coordinate?

Bits 0, 2, 4, and the remaining even Morton positions decode to x. Bits 1, 3, 5, and the remaining odd positions decode to y.

What is the largest supported Morton code?

The maximum is 18446744073709551615, or 2^64 minus one. It decodes to x = 4294967295 and y = 4294967295.

Why must I send the code as a string?

Decimal strings preserve every 64-bit integer exactly across JSON and JavaScript environments, while large JSON numbers may be rounded.

Does this convert the result to latitude and longitude?

No. It recovers unsigned integer coordinates only. Projection, scaling, offsets, and signed-coordinate rules belong to the system that created the code.

What does an API request cost?

Each API request costs $0.002. The algorithm uses no network service or probabilistic model.

Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.

POSThttps://api.kit.forhosting.com/dev/morton-decode

Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.

curl -X POST https://api.kit.forhosting.com/dev/morton-decode \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"code":"39"}'
{
  "code": "39"
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "dev.morton_decode",
  "status": "queued",
  "_links": {
    "result": "/tasks/tsk_…/result"
  }
}

The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.

Per request$0.002

Published price — no tokens, no invented credits. A failed task is never charged.

HTTPCodeMeaning
401unauthorizedMissing or invalid API key.
402insufficient_balanceYour balance doesn't cover the task price.
404unknown_typeThat task type doesn't exist.
429rate_limitedToo many requests. Use the webhook instead of polling.

Read the full KIT documentation →