PREfast Warning 291 (Windows CE 5.0)
291 - Bitwise operation on logical result.
Additional Information: ! has higher precedence than |.
Recommended Fix: Use || or (!(x | y)) instead.
The ! operator yields a Boolean result, and the | (bitwise-or) operator takes two arithmetic arguments. The ! operator also has higher precedence than |.
One of the following errors has been detected:
The expression uses parenthesis incorrectly.
Because the result of ! is Boolean (0 or 1), an attempt to test that two variables have bits set only tests that the lowest bit is present in the right side: ((!x) | y) != (!(x | y)) when x == 0 and y == 1.
The ! operator is incorrect, and should be a ~ instead.
The ! operator has a Boolean result, while the ~ operator has an arithmetic result. These operators are only interchangeable when operating on a Boolean value (0 or 1): ((!x) | y) != ((~x) | y)) when x == 1 and y == 0.
The binary operator | is incorrect, and should instead be ||.
Although | can sometimes be interchanged with ||, it is neither equivalent nor efficient because it forces evaluation of the right side of the expression.
Some side effects in this sort of expression can be terminal. For example, if (!p | (*p == '\0')), when p == NULL, it must be dereferenced to evaluate the other half of the expression.
This warning is not reported if the ! operator is on the left side of the | operator because this case is typically just the relatively benign case of an incorrect operator.
It is difficult to judge the severity of this problem without examining the code. The code should be inspected to ensure that the intended test is occurring.
This warning indicates possible confusion in the use of an operator or operator precedence.
Example
Defective Source
if (!x | y) {;}
Corrected Source
// When checking whether any bits are set in either x or y.
if (!(x | y)) {;}
// When checking whether bits are set in either the
// complement of x or in y.
if ((~x) | y) {;}
// When y is a Boolean or Boolean result.
if ((!x) || y) {;}
Send Feedback on this topic to the authors