Last active
September 20, 2025 19:22
-
-
Save jweinst1/c51d7ea98d0036f80f31044d9ec7a011 to your computer and use it in GitHub Desktop.
a way to check for nearest neighbor with bit ops and xor sets with mash
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
| >>> bin(8) | |
| '0b1000' | |
| >>> bin(12) | |
| '0b1100' | |
| >>> bin(16) | |
| '0b10000' | |
| >>> bin(16 + 0b100) | |
| '0b10100' | |
| >>> bin(16 + 0b100 + 0b100) | |
| '0b11000' | |
| >>> bin(16 + 0b100 + 0b100 + 0b100) | |
| '0b11100' |
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
| >>> s = 0b10110010 | |
| >>> mask = 0b10110010 | |
| >>> k = 0b10011 | |
| >>> bin(s & k) | |
| '0b10010' | |
| >>> start = s & k | |
| >>> start | |
| 18 | |
| >>> start = (start - 1) & mask | |
| >>> start | |
| 16 | |
| >>> bin(start) | |
| '0b10000' | |
| >>> start = (start - 1) & mask | |
| >>> bin(start) | |
| '0b10' | |
| >>> start = (start - 1) & mask | |
| >>> bin(start) | |
| '0b0' |
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
| >>> s = 0b10110010 | |
| >>> s | |
| 178 | |
| >>> mask = 0b10110010 | |
| >>> s = (s - 1) & mask | |
| >>> s | |
| 176 | |
| >>> bin(s) | |
| '0b10110000' | |
| >>> s = (s - 1) & mask | |
| >>> bin(s) | |
| '0b10100010' | |
| >>> s | |
| 162 | |
| >>> s = (s - 1) & mask | |
| >>> bin(s) | |
| '0b10100000' | |
| >>> s | |
| 160 | |
| >>> s = (s - 1) & mask | |
| >>> bin(s) | |
| '0b10010010' | |
| >>> s | |
| 146 |
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
| >>> s = 0b101100 | |
| >>> 0b1000 - 0b100 | |
| 4 | |
| >>> 0b100 + 4 | |
| 8 | |
| >>> mask = 0b101100 | |
| >>> d = 0b1000 | |
| >>> d = (d - 1) & mask | |
| >>> d | |
| 4 | |
| >>> bin(d) | |
| '0b100' | |
| >>> d = 0b1100 | |
| >>> bin(d) | |
| '0b1100' | |
| >>> d = (d - 1) & mask | |
| >>> bin(d) | |
| '0b1000' | |
| >>> d = (d - 1) & mask | |
| >>> bin(d) | |
| '0b100' | |
| >>> 0b101100 | |
| 44 | |
| >>> d = 0b101100 | |
| >>> d = (d - 1) & mask | |
| >>> bin(d) | |
| '0b101000' | |
| >>> d = (d - 1) & mask | |
| >>> bin(d) | |
| '0b100100' | |
| >>> d = (d - 1) & mask | |
| >>> bin(d) | |
| '0b100000' | |
| >>> d = (d - 1) & mask | |
| >>> bin(d) | |
| '0b1100' |
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
| void iterate_submasks(uint8_t mask) { | |
| // Start from the mask itself, then decrease | |
| for (uint8_t sub = mask; sub; sub = (sub - 1) & mask) { | |
| printf("0b"); | |
| for (int i = 7; i >= 0; --i) { | |
| printf("%d", (sub >> i) & 1); | |
| } | |
| printf("\n"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment