Skip to content

Instantly share code, notes, and snippets.

@jweinst1
Created September 26, 2025 22:02
Show Gist options
  • Save jweinst1/523cf455eac3653d5b20e6d48abc5cae to your computer and use it in GitHub Desktop.
Save jweinst1/523cf455eac3653d5b20e6d48abc5cae to your computer and use it in GitHub Desktop.
get the next greatest neighbor in bitset
#include <iostream>
#include <bitset>
int next_ge(uint64_t S, int k) {
uint64_t mask = ~((1ULL << k) - 1);
uint64_t candidates = S & mask;
return __builtin_ctzll(candidates);
}
int main(int argc, char const *argv[])
{
std::cout << size_t(next_ge(((uint64_t)1 << 32) | (1 << 26), 23)) << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment