Edit

Share via


Warning C6239

('non-zero constant' && 'expression') always evaluates to the result of 'expression'. Did you intend to use the bitwise-and operator?

This warning indicates that a non-zero constant value, other than one, was detected on the left side of a logical-AND operation that occurs in a test context. For example, the expression ( 2 && n ) is reduced to (!!n), which is the Boolean value of n.

Remarks

This warning typically indicates an attempt to check a bit mask in which the bitwise-AND (&) operator should be used, and isn't generated if the non-zero constant evaluates to 1 because of its use for selectively choosing code paths.

Code analysis name: NONZEROLOGICALAND

Example

The following code generates this warning:

#include <stdio.h>
#define INPUT_TYPE 2
void f( int n )
{
   if(INPUT_TYPE && n) // warning C6239
   {
      puts("boolean value of n is true");
   }
   else
   {
      puts("boolean value of n is false");
   }
}

To correct this warning, use bitwise-AND (&) operator as shown in the following code:

#include <stdio.h>
#define INPUT_TYPE 2
void f( int n )
{
   if( ( INPUT_TYPE & n ) )
   {
      puts("bitmask true");
   }
   else
   {
      puts("bitmask false");
   }
}

See also

& Operator