Skip to content

Instantly share code, notes, and snippets.

@int32bit
Created November 18, 2015 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save int32bit/647491ca070c4bf5226c to your computer and use it in GitHub Desktop.
Save int32bit/647491ca070c4bf5226c to your computer and use it in GitHub Desktop.
A magic method for calculating the number of binary 1
int solve(int n)
{
int sum = 0;
while (n) {
++sum;
n &= (n - 1);
}
return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment