求一个数二进制代码中1的个数
#include <stdio.h>
#include <stdlib.h>
int count_one_bit(unsigned int num)
{ int count=0;
while (num)
{
if (num % 2 == 1)
count++;
num = num / 2;
}
return count;
}
int main()
{ int ret=count_one_bit(255);
printf("%d\n", ret);
system("pause");
}
这个程序其实是C语言中很简单的一个,但是在和同学讨论的时我还是感觉自己有一些细节没有很清楚,这就代表我的C语言基础还是不扎实。所以,我感觉要学好一门语言一定要把基础打好,不在乎学得多,但是一定要消化好学过的知识。还有要多动手,这样才能找出问题才会知道需要学的还很多。
这是我的第一篇博客,这些话其实更多的是给自己说的,只有自己做到了才有资格说给别人。共勉,加油。本文出自 “sherry wang” 博客,请务必保留此出处http://940814wang.blog.51cto.com/10910665/1717935
原文:http://940814wang.blog.51cto.com/10910665/1717935