当前位置:网站首页>C language: find the number of 1 in binary stored in memory as an integer
C language: find the number of 1 in binary stored in memory as an integer
2022-07-28 03:48:00 【Gao You Wu Shao】
int main()
{
// For negative numbers, we count the number of complements 1
// such as -1
// primary :10000000 00000000 00000000 00000001
// back :11111111 11111111 11111111 11111110
// repair :11111111 11111111 11111111 11111111
//-1 Binary stored in memory 1 The number of 32
int a = 0;
printf(" Please enter the integer you want to test :");
scanf("%d", &a);
int tmp = a;
int count = 0;
if (a >= 0)
{
while (a)
{
if (a & 1) {
// You can judge the current a And then finally 1 still 0
count++;
}
a = a >> 1;
}
}
else
{
// Move the negative number right , My compiler shifts arithmetic right ( Most compilers shift arithmetic right )
// Arithmetic is moving right , Rightmost number discard , Fill in the sign on the left , Because it is negative, that is, complement 1
// If you still use it here while(a) It will continue in an endless cycle , Because the left side will always fill 1
// But we know that plastic surgery is 32 Bits stored in memory , We cycle 32 Next time
for (int i = 0;i < 32;i++)
{
if (a & 1) {
// You can judge the current a And then finally 1 still 0
count++;
}
a = a >> 1;
}
}
printf("%d Binary stored in memory 1 The number of %d", tmp,count);
return 0;
}
Screenshot of Niuke passing the test 
Links to related topics : Binary 1 The number of
边栏推荐
- 【OPENVX】对象基本使用之vx_distribution
- 面试必备杀技:SQL查询专项训练!
- 动态规划——509. 斐波那契数
- CH340 RTS DTR引脚编程驱动OLED
- Common weak network testing tools
- C语言力扣第45题之跳跃游戏 II。遍历跳跃
- [openvx] VX for basic use of objects_ convolution
- Light year admin background management system template
- Dynamic planning - 1049. Weight of the last stone II
- D2dengine edible tutorial (4) -- draw text
猜你喜欢

C语言:求一个整数存储在内存中的二进制中1的个数

Greedy - 53. Maximum subarray sum

test case management tool

The latest version of pagoda installs the zip extension, and PHP -m does not display the processing method
![[prototype and prototype chain] get to know prototype and prototype chain~](/img/8a/d6362fdd50dc883ff817a997ab9e1e.png)
[prototype and prototype chain] get to know prototype and prototype chain~

Differences among BRD, MRD and PRD

贪心——53. 最大子数组和

Appnium--APP自动化测试工具

LabVIEW loads and uses custom symbols in tree control projects

Light year admin background management system template
随机推荐
【原型与原型链】初识原型与原型链~
Prefix-Tuning: Optimizing Continuous Prompts for Generation
2022 summary of the latest Android handler related interview questions
C语言:求一个整数存储在内存中的二进制中1的个数
基于SSM实现在线租房系统
一文读懂Plato Farm的ePLATO,以及其高溢价缘由
Selenium--WEB自动化测试工具
单调栈——739. 每日温度
Prefix-Tuning: Optimizing Continuous Prompts for Generation
[错题]Concatenation
Data rich Computing: m.2 meets AI at the edge
【P4】解决本地文件修改与库文件间的冲突问题
Asemi rectifier bridge gbpc5010, gbpc5010 parameters, gbpc5010 size
CH340 RTS DTR引脚编程驱动OLED
Dynamic programming - 509. Fibonacci number
ES6 从入门到精通 # 09:Symbol 类型
4-day excel practical training camp, 0.01 yuan special offer for only three days, 200 sets of learning kits
MSGAN用于多种图像合成的模式搜索生成对抗网络---解决模式崩塌问题
Practical scripts of mangopapa (contents)
Greedy - 53. Maximum subarray sum