当前位置:网站首页>[C exercise] find the number of 1 of an integer in binary
[C exercise] find the number of 1 of an integer in binary
2022-07-18 01:40:00 【Domeecky】
The first one is
In the decimal system, if we want to get every digit of a number , This number can be iterated (%10) and (/10).

According to this conclusion , We can know , If you want to get a number of each 2 Hexadecimal number , Then you can cycle this number (%2) and (/2).

int main()
{
unsigned int num = 0;
int count = 0;
scanf("%u", &num);
while (num)
{
if (num % 2 == 1)
count++;
num /= 2;
}
printf("%d", count);
return 0;
}Because negative numbers are stored in memory in a different way from positive numbers , So we'd better use unsigned shaping to receive and input , In this way, negative numbers can also be found .
The second kind
We can also use the displacement operator to solve the problem
int main()
{
int num = 0;
int count = 0;
scanf("%d", &num);
for (int i = 0; i < 32; i++)
{
if (((num >> i) & 1) == 1)
count++;
}
printf("%d", count);
return 0;
}
The third kind of
int main()
{
unsigned int num = 0;
int count = 0;
scanf("%u", &num);
while (num)
{
num &= (num - 1);
count++;
}
printf("%d", count);
return 0;
}
We can also use the third algorithm to judge whether a number is 2 Of n Power .
int main()
{
int num = 0;
scanf("%d", &num);
if ((num & (num - 1)) == 0)
printf("%d yes 2 Of n Power \n", num);
else
printf("%d No 2 Of n Power \n", num);
return 0;
}When we take one 2 Of n Second power -1 When , Its binary will produce borrow , It will result in the sum of each digit of the original number -1 Everyone behind is different , This is the time for & Operation will get 0.

边栏推荐
- Common SQL statements, stored procedures and functions
- Common vulnerability types of tcp/ip protocol
- Online multi line text batch regular replacement add suffix tool
- 【Verilog】32位单精度浮点数比较大小
- C语言实现手机通讯录
- Network security (1)
- System. Use of arraycopy and detailed explanation of parameter meaning
- 【C 练习】打印‘X’图形
- Offset printing machine
- Redis transaction and Message Subscription Publishing
猜你喜欢

手机浏览器产品分析

Online multi line text batch regular replacement add suffix tool

STM32应用开发实践教程:环境光照强度监测的应用开发

002 descendant selector

STM32 application development practice tutorial: application development of environmental light intensity monitoring

正式发布丨VS Code 1.69

Say goodbye to Leica, Huawei opens a new era! Netizen: Xiaomi

【C】函数栈帧的创建和销毁

【C数据的存储】

STM32应用开发实践教程:环境参数持久化存储的应用开发
随机推荐
JMeter 21 day clock in day04
Redis入门介绍
Vulnerability scanning tools Nessus, rapid7 insightvm, OpenVAS installation & simple use
laravel 中 in 多列特殊查询类型解决方案
【C 练习】打印菱形
JMeter 21 day clock in day02
常用SQL语句、存储过程和函数
Offset printing machine
2020ccpc Qinhuangdao exam results (ruler)
Chrome realizes automated testing: recording and playback web page actions
MFC pet store information management system
Redis persistence - RDB
Good people are late acquaintances
C language university water, electricity and gas management system
网络安全(2)
How Axure achieves screen adaptation
坚持写下去的原因
【HCIA】数通网络基础
【C 练习】序列中删除指定的数字
STM32应用开发实践教程:环境参数持久化存储的应用开发