Rule 3–4–1(Required) An identifier declared to be an object or type shall be defined in a block that minimizes its visibility.
在尽可能小的块范围内定义变量会降低这些变量的可见性,从而降低意外使用这些标识符的可能性。这样做的一个推论是,全局对象(包括单例函数对象)应使用包含在多个函数中。
Rule 5–0–3 (Required) A cvalue expression shall not be implicitly converted to a different underlying type.
为了确保表达式中的所有操作都以相同的基础类型执行,定义为cvalue的表达式不应进行进一步的隐式转换。
mark一下大婶的求一个数字的二进制中 1 的个数写法
③通过按位与操作符(&)巧妙运算实现
Example: 当num=15时,
1//num&(num-1)=(1111)&(1110)=(1110)
2//num&(num-1)=(1110)&(1101)=(1100)
3//num&(num-1)=(1100)&(1011)=(1000)
4//num&(num-1)=(1000)&(0111)=0 ,循环停止。共执行4次while循环。
可以发现:每执行一次就去掉了最右边的1,so~循环执行几次就有几个1啦~
高效!有几个1就处理几次
代码3(优化2):
#include
int main()
{
int num = 15;
int count = 0;
int i = 0;
while (num)
{
num = num&(num - 1);
count++;
}
printf("二进制中1的个数为:%d\n",count);
return 0;
---------------------
作者:sillyxue
来源:CSDN
原文:https://blog.csdn.net/sillyxue/article/details/80287609
版权声明:本文为博主原创文章,转载请附上博文链接!
http://www.planetb.ca/syntax-highlight-word
if (inFile.fail()){
cout << "读取 VOBC-LTE-WLAN_IP 文件失败" << endl;
exit(1);
}