Created
April 22, 2014 22:49
-
-
Save supr/11197053 to your computer and use it in GitHub Desktop.
This file contains 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
#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