使用递归函数
</pre><pre name="code" class="objc">#include <stdio.h> int count; int fun(int x) { if(x==0) return 0; else { printf("%d",fun(x/2)); if (x%2 == 1) { count++; return x%2; } else { return x%2; } } } int main() { unsigned x;//一般二进制时我们只考虑正数 scanf("%d",&x); printf("%d",fun(x)); printf("\nbit为1的个数为:"); printf("%d",count); }
原文地址:http://blog.csdn.net/u011046042/article/details/42705183