TL;DR

A technical note shows how a full chess position can be encoded in roughly 26 bytes by combining a straightforward 6-bit-per-piece layout with several combinatorial and bit-level optimizations. The approach reclaims space by exploiting the fact that no two pieces share a square and by compactly encoding promotions.

What happened

The author revisited a claim that a chess position can be stored in 26 bytes. The naive representation maps 32 pieces to 64 squares, using 6 bits per piece for 192 bits (24 bytes). Additional game state—captures, castling rights, en passant targets and promotions—appears to add substantial overhead: a straightforward bitmap approach totals about 100 bits (~12 bytes), pushing the representation well past 24 bytes. By observing that no two pieces occupy the same square, the author and commenters show how captures, castling availability and en passant targets can be encoded implicitly using existing piece positions (for example, by interpreting a king’s or rook’s position in a reserved way). Promotions are handled by creating a canonical sorted encoding of promoted pawns; combinatorial counting yields 495 distinct promotion patterns per side, which fits in 9 bits per side. Combining the 24-byte baseline with the optimized promotion encoding produces a representation of roughly 26 bytes. The post links to example code on Replit and GitHub and notes related compression ideas such as Huffman coding.

Why it matters

  • Demonstrates how domain constraints (no two pieces per square) let you reclaim storage otherwise used for auxiliary flags.
  • Shows a concrete, compact representation useful for storing or transmitting large numbers of chess positions efficiently.
  • Connects discrete combinatorics and bit-level engineering to practical compression of structured game state.
  • Highlights that clever encodings can sometimes eliminate explicit metadata by interpreting positions combinatorially.

Key facts

  • A simple layout assigns each of 32 pieces to one of 64 squares, requiring 6 bits per piece (2^6 = 64) or 192 bits total — 24 bytes.
  • Naive additional state (captures, castling, en passant, promotions) can be represented with 100 bits (~12 bytes) if stored with independent bitmaps.
  • No two pieces share a square; that property is used to repurpose piece-position slots to encode captures, castling rights and en passant targets without extra bits.
  • Promotions are encoded by representing promoted pawns with small codes, sorting the codes to make the ordering unique, and counting distinct sorted strings.
  • There are 495 distinct promotion-encoding patterns per side under the chosen scheme; these fit in 9 bits per side.
  • With the optimized promotion encoding (9 bits × 2) plus the 24-byte baseline positions, the total representation is about 26 bytes.
  • The author points to runnable code on Replit and a repository on GitHub as implementations of the idea.
  • Footnotes note that Forsyth-Edwards Notation (FEN) stores additional fields such as active color, half-move clock and full-move number, which are not included in the 26-byte claim as described.

What to watch next

  • Review the linked Replit and GitHub code to confirm implementation details and edge cases.
  • Further compression approaches mentioned include applying Huffman coding to games; explore how that interacts with the 26-byte position encoding.
  • Whether the 26-byte encoding accounts for active color, half-move clock and full-move number is not confirmed in the source.

Quick glossary

  • bit: The smallest unit of digital information, representing a binary value of 0 or 1.
  • byte: A group of eight bits, commonly used as a basic unit of storage.
  • en passant: A special pawn capture in chess that is possible immediately after a pawn makes a two-square advance and an opposing pawn could have captured it as if it had moved one square.
  • castling: A chess move involving the king and one rook that simultaneously moves both pieces under specific conditions; availability depends on prior moves.
  • promotion: When a pawn reaches the farthest rank and is replaced by a knight, bishop, rook, or queen.

Reader FAQ

Does the 26-byte representation include the side to move and move counters?
not confirmed in the source

Where can I find working code for this encoding?
The author provides links to a Replit demo and a GitHub repository in the post.

How are captures, castling rights and en passant targets encoded without extra bits?
By leveraging that no two pieces share a square: certain piece positions (for example a king or a rook at a canonical square) are repurposed to signal captures or castling availability and to indicate en passant targets.

Is the promotion encoding lossless and compact?
Yes; promotions are encoded via a canonical sorted representation of promotion codes, yielding 495 distinct patterns per side which fit in 9 bits per side.

How to store a chess position in 26 bytes using bit-level magic Author's note: This post was adapted from a presentation at the Recurse Center. Hacker News comments here. This…

Sources

Related posts

By

Leave a Reply

Your email address will not be published. Required fields are marked *