You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> Warning C26813: Use 'bitwise and' to check if a flag is set
14
+
15
+
Most `enum`s with power of two member values are intended to be used with bit flags. As a result, we rarely want to equality compare these flags. We want to extract the bits we are interested in using bitwise operations instead.
16
+
17
+
## Example
18
+
19
+
```cpp
20
+
enum BitWise
21
+
{
22
+
A = 1,
23
+
B = 2,
24
+
C = 4
25
+
};
26
+
27
+
voiduseEqualsWithBitwiseEnum(BitWise a)
28
+
{
29
+
if (a == B) // Warning C26813: Use 'bitwise and' to check if a flag is set
> Warning C26827: Did you forgot to initialize an enum or wanted to use another type?
14
+
15
+
Most `enum` types used in bitwise operations are expected to have members with values of powers of two. This warning attempts to find cases where we forgot to give a value to an enum constant explicitly or inadvertently used the wrong enum type.
16
+
17
+
## Example
18
+
19
+
```cpp
20
+
enumclassAlmostBitWise
21
+
{
22
+
A = 1,
23
+
B = 2,
24
+
C = 4,
25
+
D
26
+
};
27
+
28
+
intalmostBitwiseEnums(AlmostBitWise a, bool cond)
29
+
{
30
+
return (int)a|(int)AlmostBitWise::A; // Warning C26827: Did you forgot to initialize an enum or wanted to use another type?
31
+
}
32
+
```
33
+
34
+
To fix the warning, initialize the enum constant to the correct value or use the correct enum type in the operation.
35
+
36
+
```cpp
37
+
enum class AlmostBitWise
38
+
{
39
+
A = 1,
40
+
B = 2,
41
+
C = 4,
42
+
D = 8
43
+
};
44
+
45
+
int almostBitwiseEnums(AlmostBitWise a, bool cond)
46
+
{
47
+
return (int)a|(int)AlmostBitWise::A; // No warning.
> Warning C26828: Different enum types have overlapping values. Did you want to use another enum constant here?
14
+
15
+
Most of the times we use a single enum to describe all the bit flags we can use for an option. If we use two different enum types in the same bitwise expression where the enums have overlapping values the chances are good those enums were not designed to be used in the expression.
16
+
17
+
## Example
18
+
19
+
```cpp
20
+
21
+
enum BitWiseA
22
+
{
23
+
A = 1,
24
+
B = 2,
25
+
C = 4
26
+
};
27
+
28
+
enumclassBitWiseB
29
+
{
30
+
AA = 1,
31
+
BB = 2,
32
+
CC = 4,
33
+
All = 7
34
+
};
35
+
36
+
intoverlappingBitwiseEnums(BitWiseA a)
37
+
{
38
+
return (int)a|(int)BitWiseB::AA; // Warning C26828: Different enum types have overlapping values. Did you want to use another enum constant here?
39
+
}
40
+
```
41
+
42
+
To fix the warning make sure `enum`s that are designed to be used together have no overlapping values or make sure all the related options are in a single `enum`.
0 commit comments