-
-
Save dcvz/6383993 to your computer and use it in GitHub Desktop.
Return number of 1's in the binary representation of N
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 ones(int n) | |
{ | |
// base case | |
if (n < 2) | |
return n; | |
// recursive case | |
return n % 2 + ones(n/2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment