Created
June 13, 2014 07:43
-
-
Save williame/3fcfeb1d424f01b2fa59 to your computer and use it in GitHub Desktop.
iterating over the set bits in an int efficiently
This file contains 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
for(uint32_t m = mask, i = ~0U; m; ) { | |
auto shift = __builtin_ffs(m); | |
m >>= shift; | |
i += shift; | |
... do something with i | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment