Skip to content

Instantly share code, notes, and snippets.

@tomcha
Created April 22, 2015 10:02
Show Gist options
  • Select an option

  • Save tomcha/61a63f82ad4db4cbaed2 to your computer and use it in GitHub Desktop.

Select an option

Save tomcha/61a63f82ad4db4cbaed2 to your computer and use it in GitHub Desktop.
2-9
#include <stdio.h>
int bitcount_kai(unsigned x);
int main(){
printf("%d\n", bitcount_kai(9));
}
int bitcount_kai(unsigned x){
int b = 0;
while(x != 0){
x &= (x - 1);
b++;
}
return b;
}
/*
その時点で一番右の1bitが、(&演算する都度)順に消えていく
-> 回数が1の数。
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment