Skip to content

Instantly share code, notes, and snippets.

@dotslash
Last active December 14, 2015 10:29
Show Gist options
  • Save dotslash/5072310 to your computer and use it in GitHub Desktop.
Save dotslash/5072310 to your computer and use it in GitHub Desktop.
#define _all5 0x55555555
#define _all3 0x33333333
#define _all0F 0x0F0F0F0F
#define _all01 0x01010101
#define _to64(x) (((long long)x)<<32 + x)
int NumberOfSetBits64(long long i)
{
i = i - ((i >> 1) & _to64(_all5));
i = (i & _to64(_all3)) + ((i >> 2) & _to64(_all3));
i = ((i + (i >> 4)) & _to64(_all0F));
return (i*_to64(_all01))>>56;
}
int NumberOfSetBits32(int i)
{
i = i - ((i >> 1) & _all5);
i = (i & _all3) + ((i >> 2) & _all3);
i = ((i + (i >> 4)) & _all0F);
return (i*(_all01))>>24;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment