Buggy code:
public class MagicMoveGenerator implements MoveGenerator { private int [ ] moves ; private int moveIndex ; private long all ; private long mines ; private long others ; BitboardAttacks bbAttacks ; public int generateMoves ( Board board , int [ ] moves , int mIndex ) { this . moves = moves ; bbAttacks = BitboardAttacks . getInstance ( ) ; moveIndex = mIndex ; all = board . getAll ( ) ; mines = board . getMines ( ) ; others = board . getOthers ( ) ; byte index = 0 ; long square = 1L ; while ( square != 0 ) { if ( ( board . getTurn ( ) ) == ( ( square & ( board . whites ) ) != 0 ) ) { if ( ( square & ( board . rooks ) ) != 0 ) { generateMovesFromAttacks ( ROOK , index , bbAttacks . getRookAttacks ( index , all ) ) ; } else if ( ( square & ( board . bishops ) ) != 0 ) { generateMovesFromAttacks ( BISHOP , index , bbAttacks . getBishopAttacks ( index , all ) ) ; } else if ( ( square & ( board . queens ) ) != 0 ) { generateMovesFromAttacks ( QUEEN , index , bbAttacks . getRookAttacks ( index , all ) ) ; generateMovesFromAttacks ( QUEEN , index , bbAttacks . getBishopAttacks ( index , all ) ) ; } else if ( ( square & ( board . kings ) ) != 0 ) { generateMovesFromAttacks ( KING , index , bbAttacks . king [ index ] ) ; } else if ( ( square & ( board . knights ) ) != 0 ) { generateMovesFromAttacks ( KNIGHT , index , bbAttacks . knight [ index ] ) ; } else if ( ( square & ( board . pawns ) ) != 0 ) { if ( ( square & ( board . whites ) ) != 0 ) { if ( ( ( square << 8 ) & ( all ) ) == 0 ) { addMoves ( PAWN , index , ( index + 8 ) , ( square << 8 ) , false , true , 0 ) ; if ( ( ( square & ( BitboardUtils . b2_d ) ) != 0 ) && ( ( ( square << 16 ) & ( all ) ) == 0 ) ) addMoves ( PAWN , index , ( index + 16 ) , ( square << 16 ) , false , false , 0 ) ; } generatePawnCapturesFromAttacks ( index , bbAttacks . pawnUpwards [ index ] , board . getPassantSquare ( ) ) ; } else { if ( ( ( square > > > 8 ) & ( all ) ) == 0 ) { addMoves ( PAWN , index , ( index - 8 ) , ( square > > > 8 ) , false , true , 0 ) ; if ( ( ( square & ( BitboardUtils . b2_u ) ) != 0 ) && ( ( ( square > > > 16 ) & ( all ) ) == 0 ) ) addMoves ( PAWN , index , ( index - 16 ) , ( square > > > 16 ) , false , false , 0 ) ; } generatePawnCapturesFromAttacks ( index , bbAttacks . pawnDownwards [ index ] , board . getPassantSquare ( ) ) ; } } } square <<= 1 ; index ++ ; } square = ( board . kings ) & ( mines ) ; <START_BUG> byte myKingIndex = - 1 ; <END_BUG> if ( ( ( ( all ) & ( board . getTurn ( ) ? 6L : 432345564227567616L ) ) == 0 ) && ( board . getTurn ( ) ? board . getWhiteKingsideCastling ( ) : board . getBlackKingsideCastling ( ) ) ) { myKingIndex = BitboardUtils . square2Index ( square ) ; if ( ( ( ! ( board . getCheck ( ) ) ) && ( ! ( bbAttacks . isIndexAttacked ( board , ( ( byte ) ( myKingIndex - 1 ) ) , board . getTurn ( ) ) ) ) ) && ( ! ( bbAttacks . isIndexAttacked ( board , ( ( byte ) ( myKingIndex - 2 ) ) , board . getTurn ( ) ) ) ) ) addMoves ( KING , myKingIndex , ( myKingIndex - 2 ) , 0 , false , false , TYPE_KINGSIDE_CASTLING ) ; } if ( ( ( ( all ) & ( board . getTurn ( ) ? 112L : 8070450532247928832L ) ) == 0 ) && ( board . getTurn ( ) ? board . getWhiteQueensideCastling ( ) : board . getBlackQueensideCastling ( ) ) ) { if ( myKingIndex == ( - 1 ) ) { myKingIndex = BitboardUtils . square2Index ( square ) ; } if ( ( ( ! ( board . getCheck ( ) ) ) && ( ! ( bbAttacks . isIndexAttacked ( board , ( ( byte ) ( myKingIndex + 1 ) ) , board . getTurn ( ) ) ) ) ) && ( ! ( bbAttacks . isIndexAttacked ( board , ( ( byte ) ( myKingIndex + 2 ) ) , board . getTurn ( ) ) ) ) ) addMoves ( KING , myKingIndex , ( myKingIndex + 2 ) , 0 , false , false , TYPE_QUEENSIDE_CASTLING ) ; } return moveIndex ; } private void generateMovesFromAttacks ( int pieceMoved , int fromIndex , long attacks ) { } private void generatePawnCapturesFromAttacks ( int fromIndex , long attacks , long passant ) { } private void addMoves ( int pieceMoved , int fromIndex , int toIndex , long to , boolean capture , boolean checkPromotion , int moveType ) { } No newline ; at end ; of file ; }

Fix:
int myKingIndex = - 1 ;