Skip to content

Latest commit

 

History

History
48 lines (37 loc) · 1015 Bytes

c26813.md

File metadata and controls

48 lines (37 loc) · 1015 Bytes
title description ms.date f1_keywords helpviewer_keywords
Warning C26813
Learn more about: Warning C26813
05/17/2022
C26813
USE_BITWISE_AND_TO_CHEK_ENUM_FLAGS
USE_BITWISE_AND_TO_CHECK_ENUM_FLAGS
C26813

Warning C26813

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

See also

C26827
C26828