Try to avoid calls to position legal

This avoids some calls to the somehow expensive Position::legal without adding operations to loop (bitwise and instead of comparison and logic or).
No functional change.