题目名称:统计二级制中1的个数

内容:写一个函数返回参数二进制中1的个数
列如:十进制:15 二进制: 00001111 中1的各位为4

第一种方法

int count_one(unsigned int a) {    int count = 0;    while (a) {        if (a % 2 == 1) {            count++;        }        a = a / 2;    }    return count;}int main() {    int a = 0;    scanf("%d", &a);    int count=count_one(a);    printf("count=%d", count);    return 0;}

第二种方法

int count_one(int a) {    int count = 0;    while (a) {        a = a & a - 1;        count++;    }    return count;}int main() {    int a = 0;    scanf("%d", &a);    int count=count_one(a);    printf("count=%d", count);    return 0;

}

©著作权归作者所有:来自51CTO博客作者ンめ的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. 一个数据开发人员使用的辅助工具
  2. 4-2(vector)
  3. 做运维都干什么?
  4. Spark分区器HashPartitioner和RangePartitioner代码详解
  5. 如何开始一个数据科学项目?
  6. gstvideo_has_codec: none can handle 'video/x-h264
  7. gstvideo_has_codec: none can handle 'video/x-h264
  8. gstvideo_has_codec: none can handle 'video/x-h264
  9. 从事运维工作是一种怎样的体检?

随机推荐

  1. 【Android 界面效果12】EditText中的多行
  2. Android中做一个无标题窗口
  3. 使用eclipse打开android_sdk自带的例子
  4. Android UI学习 - ListView (android.R.l
  5. android之照相、相冊裁剪功能的实现过程
  6. Android(安卓)8.1 FreeForm切换显示异常
  7. android中调试之日志
  8. Android PackageManagerService的启动过
  9. android sdk setup时出现:Failed to fetc
  10. Android不依赖Activity的全局悬浮窗实现