Skip to content

Instantly share code, notes, and snippets.

@shumon84
Created March 27, 2017 18:06
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 shumon84/e7edf3bc9a7eaf0ee63aeb01079d3d21 to your computer and use it in GitHub Desktop.
Save shumon84/e7edf3bc9a7eaf0ee63aeb01079d3d21 to your computer and use it in GitHub Desktop.
int count_bits(int n)
{
n=(n&0x55555555)+(n>>1&0x55555555);
n=(n&0x33333333)+(n>>2&0x33333333);
n=(n&0x0f0f0f0f)+(n>>4&0x0f0f0f0f);
n=(n&0x00ff00ff)+(n>>8&0x00ff00ff);
n=(n&0x0000ffff)+(n>>16&0x0000ffff);
return n;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment