title | description | ms.date | f1_keywords | helpviewer_keywords | ||||
---|---|---|---|---|---|---|---|---|
Warning C26813 |
Learn more about: Warning C26813 |
05/17/2022 |
|
|
Use 'bitwise and' to check if a flag is set
Most enum
types with power of two member values are intended to be used as bit flags. As a result, you rarely want to compare these flags for equality. Instead, extract the bits you're interested in by using bitwise operations.
Code analysis name: USE_BITWISE_AND_TO_CHEK_ENUM_FLAGS
enum BitWise
{
A = 1,
B = 2,
C = 4
};
void useEqualsWithBitwiseEnum(BitWise a)
{
if (a == B) // Warning C26813: Use 'bitwise and' to check if a flag is set
return;
}
To fix the warning, use bitwise operations:
void useEqualsWithBitwiseEnum(BitWise a)
{
if (a & B) // Fixed.
return;
}