#define _CRT_SECURE_NO_WARNINGS 1
//写一个函数返回参数二进制中 1 的个数
//比如: 15 0000 1111 4 个 1
//程序原型:
//int count_one_bits(unsigned int value)
//{
// // 返回 1的位数
//}
#include <stdio.h>
#include <stdlib.h>
int count_one_bit(unsigned int n)
{
int count = 0;
while(n)
{
if(n%2 == 1)
count++;
n = n/2;
}
return count;
}
int main()
{
int num = 0;
printf("请输入:>");
scanf("%d",&num);
int ret = count_one_bit(num);
printf("ret = %d\n", ret);
system("pause");
return 0;
}
【C语言】写一个函数返回参数二进制中 1 的个数 比如: 15 0000 1111 4 个 1 程序原型: int count_one_bits(unsigned int value)
猜你喜欢
转载自blog.csdn.net/qq_41562665/article/details/80659588
今日推荐
周排行