Skip to content

Instantly share code, notes, and snippets.

@3ki5tj
Created December 8, 2014 21:11
Show Gist options
  • Save 3ki5tj/f4da358180630d4c037d to your computer and use it in GitHub Desktop.
Save 3ki5tj/f4da358180630d4c037d to your computer and use it in GitHub Desktop.
bitwise operations
/*
http://graphics.stanford.edu/~seander/bithacks.html
*/
int is_power_of_two(unsigned int x)
{
return ((x != 0) && !(x & (x - 1)));
}
int count_bits(unsigned int v)
{
unsigned int c; // c accumulates the total bits set in v
for (c = 0; v; c++)
v &= v - 1; // clear the least significant bit set
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment