Skip to content

Instantly share code, notes, and snippets.

@lee-dohm
Created September 1, 2014 01:02
Show Gist options
  • Save lee-dohm/7a60006f9171fc7cb3cf to your computer and use it in GitHub Desktop.
Save lee-dohm/7a60006f9171fc7cb3cf to your computer and use it in GitHub Desktop.
Code for blog entry: "Interview Question Pitfalls"
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