C6239

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.

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

Example

The following code generates this warning:

#include <stdio.h>
#define INPUT_TYPE 2
void f( int n )
{
   if(INPUT_TYPE && n) // warning 6239
   {
      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

Reference

& Operator (C# Reference)