in reply to [OT] Is It Possible To Serialize A Chess Board In Fewer Than 23 Bytes?

==== Naive approach

64pos=2**6bits x 32 pieces = 24 bytes

Plus extra bits for special info, I'll ignore (I think you forgot promotion of pawns at the last rank °)

==== Anyway...

the naive approach is not optimal, since 2 pieces can't be on the same field.

So probably run length encoding of empty fields or Huffman coding are better on average.

You can tell what the optimal limit is by enumerating all possible scenarios.

The the ceiling of log2 count tells you the lower bound of needed bits.

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery

°) in theory you can have 9 queens or 10 bishops.

Update

The chess exchange discussion choroba linked to is quite good.

I have a hunch that a 23 bytes maximum is possible (on average even less), but it would require advanced encoding techniques.

For instance are bishops restricted to 32 positions each.

And castleing is only possible if a rook is in the corner.

Pawns can't reach all fields.

  • Comment on Re: [OT] Is It Possible To Serialize A Chess Board In Fewer Than 23 Bytes?

Replies are listed 'Best First'.
Re^2: [OT] Is It Possible To Serialize A Chess Board In Fewer Than 23 Bytes?
by ikegami (Patriarch) on Mar 11, 2026 at 00:19 UTC

    I think you forgot promotion of pawns at the last rank

    Maybe, maybe not. Either way, his approach correctly handles promotions.

    As for you, it's not just promotions you don't handle. You don't support capturing either! You'd need a length prefix or more bits to encode that a piece is off the board.

      I thought about capturing but the "naive (sic) approach" got already long.

      > You'd need a length prefix or more bits to encode that a piece is off the board.

      Nope. Two pieces can't be on the same field and at least one king must be left. Just position a piece on the (remaining) king to encode capturing.

      This discussion is getting useless anyway, BC if the complexity of the task and fuzzy edge cases.

      I'd like to see a count of possible board combinations first.

      update

      IIRC you have also to encode if a match stops because of resignation or a draw or a stalemate...

      So many details.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

        IIRC you have also to encode if a match stops because of resignation or a draw or a stalemate...

        As previously mentioned, if an end state is desired, a special value for the leading bitmap could be used.