Skip to content

Instantly share code, notes, and snippets.

@gigamonkey
Created February 17, 2026 19:41
Show Gist options
  • Select an option

  • Save gigamonkey/71b42c11c04bedd88d2a9d29cdad2c7d to your computer and use it in GitHub Desktop.

Select an option

Save gigamonkey/71b42c11c04bedd88d2a9d29cdad2c7d to your computer and use it in GitHub Desktop.
Bit twiddling eight-direction loop.
// 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