位掩码未返回正确的值
我正在尝试解决此问题,并且我总是得到8作为返回值。我不确定是否已读取位操作的条件。
int outputbyte[3] = {10,11,12};
int result;
result = (outputbyte[1] & 11)?8:0;
printf("\nMasked value is: %d", result);
我不明白这种情况如何发生(outputbyte [1]和11)?
答案 0 :(得分:1)
这里
int outputbyte[3] = {10,11,12};
result = (outputbyte[1] & 11)?8:0;
这个
result = (outputbyte[1] & 11)?8:0 /* (outputbyte[1] & 11) results in true hence 8 assigned to result */
是三元运算符,即第一个 operand-1 即(outputbyte[1] & 11)?8:0)
被求值,如果结果为非零,则 operand-2 即8
被分配给result
,否则 operand-3 即0
被分配给result
。
outputbyte[1] ==> 11 => 0000 1011
& ( bitwise AND operator)
11 => 0000 1011
-----------
0000 1011 => 11 i.e nonzero i.e condition true i.e 11 gets assigned to result
-----------
我希望您知道按位AND &
运算符的真值表
A B A&B
------------
0 0 0
0 1 0
1 0 0
1 1 1