Skip to content

Instantly share code, notes, and snippets.

@kulp
Created May 21, 2009 14:27
Show Gist options
  • Save kulp/115492 to your computer and use it in GitHub Desktop.
Save kulp/115492 to your computer and use it in GitHub Desktop.
/* Find set bits by value. process_faster() is between 40% and 200% faster than process_naive(), depending on optimizations and environment. */
void process_naive(long long x)
{
long long val = 1;
while (x) {
if (x & 1)
record(val);
x >>= 1;
val <<= 1;
}
}
void process_faster(long long x)
{
while (x) {
int val = x ^ (x & (x - 1));
record(val);
x &= ~val;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment