Skip to content

Latest commit

 

History

History
50 lines (40 loc) · 1.12 KB

c6299.md

File metadata and controls

50 lines (40 loc) · 1.12 KB
description title ms.date f1_keywords helpviewer_keywords ms.assetid
Learn more about: Warning C6299
Warning C6299
11/04/2016
C6299
BITFIELD_TO_BOOL_COMPARISON
__WARNING_BITFIELD_TO_BOOL_COMPARISON
C6299
5129ac34-0d4f-4056-aea2-b0df2127dead

Warning C6299

Explicitly comparing a bit field to a Boolean type will yield unexpected results

Remarks

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.

Code analysis name: BITFIELD_TO_BOOL_COMPARISON

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...
  }
}