Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Use 'bitwise and' to check if a flag is set
Remarks
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
Example
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;
}