C6316

 

For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.

For the latest documentation on Visual Studio 2017, see C6316 on docs.microsoft.com. warning C6316: Incorrect operator: tested expression is constant and non-zero. Use bitwise-and to determine whether bits are set

This warning indicates the use of bitwise-or (|) when bitwise-and (&) should have been used. Bitwise-or adds bits to the resulting expression, whereas bitwise-and selects only those bits in common between its two operators. Tests for flags must be performed with bitwise-and or a test of equality.

The following code generates this warning:

#define INPUT_VALUE 2  
void f( int Flags)  
{  
  if (Flags | INPUT_VALUE) // warning  
  {  
    // code  
  }  
}  

To correct this warning, use the following code:

#define ALLOWED 1  
#define INPUT_VALUE 2  
  
void f( int Flags)  
{  
  if ((Flags & INPUT_VALUE) == ALLOWED)  
  {  
    // code  
  }  
}  

Show: