Created
September 1, 2014 01:02
-
-
Save lee-dohm/7a60006f9171fc7cb3cf to your computer and use it in GitHub Desktop.
Code for blog entry: "Interview Question Pitfalls"
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
long count_bits(long number) { | |
// Accumulates the total bits set in `number` | |
unsigned int count; | |
// This loop initializes the count, loops only until all bits have been cleared and | |
// increments the count each time through the loop. | |
for (count = 0; number; count++) { | |
// Clear the least significant bit set | |
number &= number - 1; | |
} | |
return count; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment