Skip to content

Instantly share code, notes, and snippets.

@supr
Created April 22, 2014 22:49
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 supr/11197053 to your computer and use it in GitHub Desktop.
Save supr/11197053 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdlib>
int num_bits2(long n) {
int i = 0;
for(i = 0; n; i++)
n &= n - 1;
return i;
}
int num_bits(long n) {
int i = 0;
for(;n;) {
i += n%2;
n /= 2;
}
return i;
}
int main(int argc, char **argv) {
std::cout << num_bits(std::atoi(argv[1])) << std::endl;
std::cout << num_bits2(std::atoi(argv[1])) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment