Created
February 17, 2026 19:41
-
-
Save gigamonkey/71b42c11c04bedd88d2a9d29cdad2c7d to your computer and use it in GitHub Desktop.
Bit twiddling eight-direction loop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Get dr, dc values to move in all eight directions in a 2d array | |
| // with a loop that only iterates exactly eight times. | |
| for (int i = 0; i < 8; i++) { | |
| int dr = (i & 3) == 0 ? 0 : (1 - ((i & 4) >> 1)); | |
| int dc = (i - 2 & 3) == 0 ? 0 : (1 - ((i - 2 & 4) >> 1)); | |
| IO.println("%2d, %2d".formatted(dr, dc)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment