Created
November 18, 2015 15:51
-
-
Save int32bit/647491ca070c4bf5226c to your computer and use it in GitHub Desktop.
A magic method for calculating the number of binary 1
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
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