I'm attempting to use Zobrist hashing in my chess engine, but I'm running into some difficulties getting it to work correctly.

I'm using the random numbers from this site so I can test my results against the answers they have, and for one of the positions I can't figure out what series of XORs need to be performed to get the next correct position.

I'm attempting to go from the "position after e2e4 d7d5 e4e5" (with a hash 662fafb965db29d4) to the "position after e2e4 d7d5 e4e5 f7f5" (with the hash 22a48b5a8e47ff78).

But I can't figure out what series of XORs I need to use to go between the first position's hash key and the second (this is important as I need to incrementally add and remove pieces from the hash correctly for the Zobrist hashing to efficiently work with my engines DoMove and UndoMove functions). As far as I can tell, the black pawn on f7 is XORed away, and then a black pawn at f5 is XORed back. And then the random number for the side to move should be flipped. Here is what I said in pseduo-code:

startingHash = 662fafb965db29d4
startingHash = startingHash XOR random number for pawn at f7
startingHash = startingHash XOR random number for pawn at f5
startingHash = startingHash XOR random number for white to move

But doing this isn't giving 22a48b5a8e47ff78. Any idea where I'm going wrong here? I'm fairly confident I'm using the correct random numbers according to the spec in the link above. And I've already gotten many of the other positions to work as well. But I'm stuck on this one.

1 Answer 1


After 2...f5 white can take en passant: 3.e5xf6.You have to take this into account.

  • Right, but per the spec, "If the opponent has performed a double pawn push and there is now a pawn next to it belonging to the player to move then." I thought this meant, I suppose incorrectly, that we needed to see if the current color had any en passant opportunities, bot the color about to move.
    – Chris
    Commented Jun 16, 2021 at 1:19
  • 2
    @ChristianDean just after a pawn double push, the tentative EP square (or just its file) is stored in the board data, so that's when you factor in the EP zobrist hash. Commented Jun 16, 2021 at 10:54
  • Hmm, ok. I see. Thanks!
    – Chris
    Commented Jun 16, 2021 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.