C6290
warning C6290: Bitwise operation on logical result: ! has higher precedence than &. Use && or (!(x & y)) instead
This warning indicates possible confusion in the use of an operator or an operator precedence.
The ! operator yields a Boolean result, and it has higher precedence than the &.The bitwise-and (&) operator takes two arithmetic arguments. Therefore, one of the following errors has been detected:
The expression is mis-parenthesised:
Because the result of ! is Boolean (zero or one), an attempt to test that two variables have bits in common will only end up testing that the lowest bit is present in the right side: ((!8) & 1) == 0.
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 never interchangeable, even when operating on a Boolean value (zero or one): ((!0x01) & 0x10) == 0x0, while ((~0x01) & 0x10) == 0x10.
The binary operator & is incorrect, and should instead be &&:
While & can sometimes be interchanged with &&, it is not equivalent because it forces evaluation of the right side of the expression. Certain side effects in this type of expression can be terminal.
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.
Example
The following code generates this warning:
void f(int x, int y)
{
if (!x & y)
{
// code ..
}
}
To correct this warning, use the following sample code:
void f(int x, int y)
{
/* When testing that x has no bits in common with y. */
if (!(x & y))
{
// code
}
/* When testing for the complement of x in y. */
if ((~x) & y)
{
// code ...
}
}
#include <windows.h>
void fC(int x, BOOL y )
{
/* When y is a Boolean or Boolean result. */
if ((!x) && y)
{
// code ...
}
}
Warning C6317 is reported if the ! operator is on the right side of the & operator.