Created
December 8, 2014 21:11
-
-
Save 3ki5tj/f4da358180630d4c037d to your computer and use it in GitHub Desktop.
bitwise operations
This file contains hidden or 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
/* | |
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