Skip to content

Instantly share code, notes, and snippets.

@williame
Created June 13, 2014 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save williame/3fcfeb1d424f01b2fa59 to your computer and use it in GitHub Desktop.
Save williame/3fcfeb1d424f01b2fa59 to your computer and use it in GitHub Desktop.
iterating over the set bits in an int efficiently
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