首页 > 其他 > 详细

编写一个函数,求出整型数中bit为1的数的个数

时间:2015-01-14 09:47:15      阅读:265      评论:0      收藏:0      [点我收藏+]

使用递归函数

</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);
}
技术分享


编写一个函数,求出整型数中bit为1的数的个数

原文:http://blog.csdn.net/u011046042/article/details/42705183

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!