Created
May 21, 2009 14:27
-
-
Save kulp/115492 to your computer and use it in GitHub Desktop.
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
/* 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