Skip to content

Instantly share code, notes, and snippets.

@dcvz
Last active December 21, 2015 23:39
Show Gist options
  • Save dcvz/6383993 to your computer and use it in GitHub Desktop.
Save dcvz/6383993 to your computer and use it in GitHub Desktop.
Return number of 1's in the binary representation of N
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