C程序对整数中设置为1的位数进行计数
Problem statement: Write a C program to count number of bits set to 1 in an Integer.
问题陈述:编写一个C程序来计算Integer中设置为1的位数 。
Solution: We can use bitwise operator here to solve the problem.
解决方案:我们可以在这里使用按位运算符来解决问题。
Pre-requisite: Input number n
前提条件 :输入数字n
Algorithm:
算法:
1) Set count=1
2) Do bit wise AND with n and 1.
n & 1
let n be a7a6a5a4a3a2a1a0
1->00000001
So doing bitwise AND (refer to published article on bitwise operators)
will result in all bits 0 except the LSB which will be a0.
If a0 is 0 then the all bits are not set
Thus,
IF
n& 1 == 1
count++;
END IF
Right shift n by 1
n=n>>1
3) IF n==0
Print count
ELSE
REPEAT step 1, 2
Example with Explanation:
解释示例:
Checking for 7
7->00000111
Initially, count=0
So first iteration:
n=7 //00000111
n & 1 =1
so , count=1
n=n>>1 (n=3) //00000011
So second iteration:
n=3 //00000011
n & 1 =1
count=2
n=n>>1 (n=1) //00000001
So third iteration:
n=1 //00000001
n & 1 =1
count=3
n=n>>1 (n=0) //00000000
So, Print count, 3
C implementation
C实现
#include <stdio.h>
int main()
{unsigned int n;
printf("enter the integer\n");
scanf("%d",&n);
int count=0;
while(n!=0){if(n & 1 == 1){ //if current bit 1
count++;//increase count
}
n=n>>1;//right shift
}
printf("no of bits those are 1 ");
printf("in its binary representation: %d\n",count);
return 0;
}
Output
输出量
First run:
enter the integer
7
no of bits those are 1 in its binary representation: 3
Second run:
enter the integer
12
no of bits those are 1 in its binary representation: 2
翻译自: https://www.includehelp.com/c-programs/count-number-of-bits-set-to-1-in-an-integer.aspx
C程序对整数中设置为1的位数进行计数相关推荐
- 控制计算机硬件和修改桌面布局的应用程序,Windows 7中设置、控制计算机硬件配置和修改桌面布局的应用程序是 ( )。...
答案 查看答案 解析: [解析题]构成计算机硬件系统的主要部件有: [解析题]某种计算机的内存容量是8G,这里的8G容量是指 ( )K字节 [解析题]神威·太湖之光超级计算机是我国首台具有完全自主知识 ...
- android设置成默认应用程序,在Android中设置和取消设置默认应用
我有一个尝试"进入信息亭模式"的应用程序,但是我只希望它仅出现在一个Activity上.在修改了一些控件之后,我想到了Intent.createChooser(). 我想做的是,一 ...
- 微信小程序--在wxml中设置保留小数位数
需求 在wxml中计算价格*数量小数出现问题. 需要新建一个wxs文件 var filters = {toFix: function (value) {return value.toFixed(2)/ ...
- 在小程序 Editor编辑器中 设置初始值, 获取edotor的值
一.场景:直接在Edotor副文本编辑器中渲染数值,用于修改数据,可以直接编辑的那种,如图 图中的副文本编辑器中的值,就是初次拉去的数值填充到这里面的 二.下面介绍一下 怎样实现上面的(需要用到的方法 ...
- 在微信小程序开发软件中设置字体大小问题
我一开始接触微信小程序开发的时候,只是知道代码区的字体大小怎么调节(因为字体太小了,看得很累),于是我在网上查找方法想调节调试器那里的字体(字体太小看得很累).但是网上面都没有具体的方法,然后我就这样 ...
- php中date设置北京时区,PHP中设置时区方法小结
PHP代码中的时间判断语句出了点问题,问题出在PHP的时区设置上,PHP所取的时间默认是格林威治标准时间,所以和北京时间相差8小时.找到原因后,在网上搜索到了一些关于PHP的时区设置方法:1.修改ph ...
- python程序设置_Windows系统中设置Python程序定时运行方法
Windows系统中设置Python程序定时运行方法 一.环境 win7 + Python3.6 二.步骤 1,在Windows开始菜单中搜索"计划任务",并且点击打开" ...
- Linux下设置时区(通过shell设置和程序中设置)及程序中设置环境变量
Shell中设置 bash中 export TZ="Europe/Moscow" date -u -s "2011-10-29 21:55:00&quo ...
- 微信小程序(购物车)--在wxml中设置保留小数位数
微信小程序(购物车)–在wxml中设置保留小数位数 一.在该页面文件夹下新建一个wxs后缀的文件 var filters = {toFix: function (value) {return valu ...
最新文章
- java 获取泛型_Java泛型 | Jackson TypeReference获取泛型类型信息
- 量子计算陷入难解困境,未来发展何去何从?
- 抓住二维核力的尾巴---将粒子
- centos6.8 安装mysql_Centos6.8通过yum安装mysql5.7
- python语句print(tuple(range(2)))_Python学习(四)数据结构 —— list tuple range
- C# DES加密类,16位的加密。
- speedtest-cli命令行下测试服务器外网速度
- 【易语言】贴吧工具源码 以前写的
- Python使用正则表达式检查书稿中不应该出现的重复字
- Nagios配置监控windows客户端
- 加密芯片在水电气表行业内的应用
- HTML中Css补充资料
- 20款超酷的404错误页面(上)
- 闲论排样/排料算法(nesting)
- 面试总结-2023届安全面试题总汇
- uni-app运行到微信开发工具无法预览
- (十四)商品详情页实现
- 基于Processing的躲避球游戏
- 一步一步制作最简单的动画
- 【云原生】-Docker部署SQL Server及最佳应用