Created
April 22, 2015 10:02
-
-
Save tomcha/61a63f82ad4db4cbaed2 to your computer and use it in GitHub Desktop.
2-9
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
| #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