用C语言编写一个函数返回参数二进制中 1 的个数(3种方法)(谷歌笔试题)
2017-07-29 18:00
549 查看
比如: 15 0000 1111 4 个 1
程序原型:
int count_one_bits(unsigned int value)
{
// 返回 1的位数
}
1.用模2除2的方法
(1)正数
11
//00000000 00000000 00000000 00001011--11的二进制
//11%2==1 //有1
//11/2=5 //00000000 00000000 00000000 00000101--5的二进制,尾部丢掉一个1
//5%2==1 //有1
//5/2==2 //00000000 00000000 00000000 00000010--2的二进制,尾部丢掉一个1
//2%2==0 //没有1了,跳过末尾的0,继续往前找,看还有没有1
//2/2==1 //00000000 00000000 00000000 00000001--1的二进制,尾部丢掉一个1
//1%2==1 //还有1
//1/2==0 //00000000 00000000 00000000 00000000--0的二进制,尾部丢掉一个1,彻底没有1了
test.c
#include<stdio.h>
int count_one_bits(unsigned int n)
{
int count = 0;
while (n!=0)//二进制数中必有1
{
if (n % 2 == 1)//二进制中发现1
{
count++;
}
n = n / 2;//去掉末尾的一个1
}
return count;
}
int main()
{
int num = 11;
int ret = count_one_bits(num);
printf("ret=%d\n", ret);
system("pause");
return 0;
}
运行界面
![](https://img-blog.csdn.net/20170729194118590?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmFpZHVfMzk2MzAxOTM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
(2)负数
unsigned只是从语言上看的,而在内存里只看地址
test.c
#include<stdio.h>
int count_one_bits(unsigned int n)//看待-1是2^32-1
{
int count = 0;//计数
while (n!=0)//二进制数中必有1
{
if (n % 2 == 1)//二进制中发现1
{
count++;
}
n = n / 2;//去掉末尾的一个1
}
return count;
}
int main()
{
int num = -1;//存的是补码,有符号
//10000000 00000000 00000000 00000001---原码
//11111111 11111111 11111111 11111110---反码
//11111111 11111111 11111111 11111111---补码
int ret = count_one_bits(num);
printf("ret=%d\n", ret);
system("pause");
return 0;
}
运行界面
![](https://img-blog.csdn.net/20170729200935295?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmFpZHVfMzk2MzAxOTM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
2.右移的方法
*与1按位与,最低位为0,代表原数的最低位为0;[b]最低位为1,代表原数的最低位为1。[/b]
[b]*>>箭头向右,右移操作符。[/b]
11
//00000000 00000000 00000000 00001011--11的二进制
//00000000 00000000 00000000 00000101--5的二进制,向右移一位,前面补上符号位(正0负1)。
test.c
#include<stdio.h>
int count_one_bits(unsigned int n)//看待-1是2^32-1
{
int count = 0;//计数
int i = 0;
for (i = 0; i < 32; i++)//32位平台上
{
if ((n & 1) == 1)//==优先于&
{
count++;
}
}
n = n >> 1;//在没赋值之前表达式结果为5,但n仍为11.
return count;
}
int main()
{
int num = 11;
int ret = count_one_bits(num);//返回值接受
printf("ret=%d\n", ret);
system("pause");
return 0;
}
3.n&(n-1)的方法(较优解,即会得分解)
11
//00000000 00000000 00000000 00001011--11的二进制
//00000000 00000000 00000000 00001010--10的二进制
//00000000 00000000 00000000 00001010--按位与,10,去掉结尾的1
//00000000 00000000 00000000 00001001--9的二进制
//00000000 00000000 00000000 00001000--按位与,8,去掉结尾的1
//00000000 00000000 00000000 00000111--7的二进制
//00000000 00000000 00000000 00000000--按位与,0,去掉结尾的1
执行了3次,就有3 个1。
test.c
#include<stdio.h>
int count_one_bits(unsigned int n)//看待-1是2^32-1
{
int count = 0;//计数
while (n!=0)//在n!=0之前,n = n&(n - 1)被执行几次,就有几个1.
{
count++;
n = n&(n - 1);//去掉1
}
return count;
}
int main()
{
int num = 11;
int ret = count_one_bits(num);
printf("ret=%d\n", ret);
system("pause");
return 0;}
*注意:要正数、负数都代入进行测试。
*********************************************************亮点*******************************************************
a22c
PS:能不能判断一个数字是2^n ----------用n&(n-1)=0
期待各位大神的批评指正 0^_*0
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
程序原型:
int count_one_bits(unsigned int value)
{
// 返回 1的位数
}
1.用模2除2的方法
(1)正数
11
//00000000 00000000 00000000 00001011--11的二进制
//11%2==1 //有1
//11/2=5 //00000000 00000000 00000000 00000101--5的二进制,尾部丢掉一个1
//5%2==1 //有1
//5/2==2 //00000000 00000000 00000000 00000010--2的二进制,尾部丢掉一个1
//2%2==0 //没有1了,跳过末尾的0,继续往前找,看还有没有1
//2/2==1 //00000000 00000000 00000000 00000001--1的二进制,尾部丢掉一个1
//1%2==1 //还有1
//1/2==0 //00000000 00000000 00000000 00000000--0的二进制,尾部丢掉一个1,彻底没有1了
test.c
#include<stdio.h>
int count_one_bits(unsigned int n)
{
int count = 0;
while (n!=0)//二进制数中必有1
{
if (n % 2 == 1)//二进制中发现1
{
count++;
}
n = n / 2;//去掉末尾的一个1
}
return count;
}
int main()
{
int num = 11;
int ret = count_one_bits(num);
printf("ret=%d\n", ret);
system("pause");
return 0;
}
运行界面
(2)负数
unsigned只是从语言上看的,而在内存里只看地址
test.c
#include<stdio.h>
int count_one_bits(unsigned int n)//看待-1是2^32-1
{
int count = 0;//计数
while (n!=0)//二进制数中必有1
{
if (n % 2 == 1)//二进制中发现1
{
count++;
}
n = n / 2;//去掉末尾的一个1
}
return count;
}
int main()
{
int num = -1;//存的是补码,有符号
//10000000 00000000 00000000 00000001---原码
//11111111 11111111 11111111 11111110---反码
//11111111 11111111 11111111 11111111---补码
int ret = count_one_bits(num);
printf("ret=%d\n", ret);
system("pause");
return 0;
}
运行界面
2.右移的方法
*与1按位与,最低位为0,代表原数的最低位为0;[b]最低位为1,代表原数的最低位为1。[/b]
[b]*>>箭头向右,右移操作符。[/b]
11
//00000000 00000000 00000000 00001011--11的二进制
//00000000 00000000 00000000 00000101--5的二进制,向右移一位,前面补上符号位(正0负1)。
test.c
#include<stdio.h>
int count_one_bits(unsigned int n)//看待-1是2^32-1
{
int count = 0;//计数
int i = 0;
for (i = 0; i < 32; i++)//32位平台上
{
if ((n & 1) == 1)//==优先于&
{
count++;
}
}
n = n >> 1;//在没赋值之前表达式结果为5,但n仍为11.
return count;
}
int main()
{
int num = 11;
int ret = count_one_bits(num);//返回值接受
printf("ret=%d\n", ret);
system("pause");
return 0;
}
3.n&(n-1)的方法(较优解,即会得分解)
11
//00000000 00000000 00000000 00001011--11的二进制
//00000000 00000000 00000000 00001010--10的二进制
//00000000 00000000 00000000 00001010--按位与,10,去掉结尾的1
//00000000 00000000 00000000 00001001--9的二进制
//00000000 00000000 00000000 00001000--按位与,8,去掉结尾的1
//00000000 00000000 00000000 00000111--7的二进制
//00000000 00000000 00000000 00000000--按位与,0,去掉结尾的1
执行了3次,就有3 个1。
test.c
#include<stdio.h>
int count_one_bits(unsigned int n)//看待-1是2^32-1
{
int count = 0;//计数
while (n!=0)//在n!=0之前,n = n&(n - 1)被执行几次,就有几个1.
{
count++;
n = n&(n - 1);//去掉1
}
return count;
}
int main()
{
int num = 11;
int ret = count_one_bits(num);
printf("ret=%d\n", ret);
system("pause");
return 0;}
*注意:要正数、负数都代入进行测试。
*********************************************************亮点*******************************************************
a22c
PS:能不能判断一个数字是2^n ----------用n&(n-1)=0
期待各位大神的批评指正 0^_*0
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
相关文章推荐
- 【C语言】编写一个函数返回参数二进制中1的个数。
- 我用C语言 写一个函数返回参数二进制中 1 的个数的三种不同方法
- 1、写一个函数返回参数二进制中 1 的个数(三种方法)2、获取一个数二进制序列中所有的偶数位和奇数位,分别输出二进制序列3、输出一个整数的每一位。4、两个int(32位)整数m和n的二进制表达中,有多
- c语言实现谷歌笔试题:返回参数二进制中1的个数
- 【c语言】为下面的函数原型编写函数定义,这个字符串参数必须包含一个或者多个数字,函数应该把这些数字字符转换为整数并返回这个整数。
- 【c语言】 写一个函数返回参数二进制中 1 的个数
- 【C语言】写一个函数返回参数二进制中1的位数。
- 【C语言】为下面的函数原型编写函数定义: int ascii_to_integer(char *str); 这个字符串参数必须包含一个或者多个数字,函数应该把这些数字字符转换为整数并返回这个整数。
- 用C语言写一个函数返回参数二进制中1的个数
- C语言中写一个函数返回参数二进制中 1 的个数
- C语言中写一个函数返回参数二进制中 1 的个数
- 【c语言】 写一个函数返回参数二进制中 1 的个数
- C语言 编写函数 unsigned int reverse_bit(unsigned int value); 这个函数的返回值value的二进制位模式从左到右翻转后的值。
- C语言 写一个函数返回参数值二进制为1的个数
- 【c语言】编写一个函数reverse_string(char * string) 实现:将参数字符串中的字符反向排列。 要求:不能使用C函数库中的字符串操作函数
- 【C语言】编写一个函数reverse_string(char * string) 实现:将参数字符串中的字符反向排列。要求:不能使用C函数库中的字符串操作函数。
- 编写一个名为identity_matrix的函数,他接受一个 10*10整型矩阵为参数,并返回一个布尔值,提示该矩阵是不 是单位矩阵。
- C语言 编写一个函数reverse_string(char * string) 实现:将参数字符串中的字符反向排列。 要求:不能使用C函数库中的字符串操作函数。
- SQL中函数返回表,传递一个参数,无法唤醒用户自定义函数,解决方法!
- 编写一个函数 int count_chars(char const *str,char const *chars) 函数应该在第一个参数中进行查找, 并返回匹配第二个参数所包含的字符的数量。