Partager via


C6299

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

warning C6299: explicitly comparing a bit field to a Boolean type will yield unexpected results

This warning indicates an incorrect assumption that Booleans and bit fields are equivalent. Assigning 1 to bit fields will place 1 in its single bit; however, any comparison of this bit field to 1 includes an implicit cast of the bit field to a signed int. This cast will convert the stored 1 to a -1 and the comparison can yield unexpected results.

Example

The following code generates this warning:

struct myBits  
{  
  short flag : 1;  
  short done : 1;  
  //other members  
} bitType;  
  
void f( )  
{  
  if (bitType.flag == 1)   
  {  
  // code...  
  }  
}  

To correct this warning, use a bit field as shown in the following code:

void f ()  
{  
  if(bitType.flag==bitType.done)  
  {  
    // code...  
  }  
}