[英]Bit Testing.Counting the bits
Why does it print that bit number 6 of 47(00101111) is 1.Counting the bits starts from the right side starting with 1? 为什么打印47(00101111)的第6位为1。从右边开始,从1开始计数?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int mask1,mask2;
//bit testing;
mask1=1<<3;
mask2=1<<6;
if(47&mask1!=0)
printf("\n bit number 3 is 1");
else
printf("\n bit number 3 is 0");
if(47&mask2!=0)
printf("\n bit number 6 is 1");
else
printf("\n bit number 6 is 0");
return 0;
}
The bitwise AND operator &
has lower precedence than the inequality operator !=
. 按位AND运算符
&
优先级低于不等式运算符!=
。 So this: 所以这:
47&mask1!=0
Is the same as: 是相同的:
47&(mask1!=0)
Add parenthesis as follows: 如下添加括号:
(47&mask1)!=0
And you'll get the expected results. 您将获得预期的结果。 Be sure to do the same for the check against
mask2
. 确保对
mask2
进行相同的检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.