Since mask values are integers we can represent them in different ways. For example, as macros:
#define ENABLE_FEATURE_A 0x1
#define ENABLE_FEATURE_B 0x2
#define ENABLE_FEATURE_C 0x4
#define ENABLE_FEATURE_D 0x8
Or enums:
typedef enum {
FEATURE_A = 0x1,
FEATURE_B = 0x2,
FEATURE_C = 0x4,
FEATURE_D = 0x8
} enabled_features_t
I argue that the enum representation is not appropriate for masks because masks are intended to be combined (with bitwise operations). C requires casting of enums to int because we aren't expected to perform operations on numbers that may not have a meaning. So I would think using macros is more appropriate for flags. Am I overthinking this?
int
constants, just like macros. They are for all intents and purposes plainint
values. So with your code, there's no difference between the macroENABLE_FEATURE_A
and the enumeration symbolFEATURE_A
.int
constants, and similar to macros they are compile-time constants that are replaced by the compiler. So assuming the enumeration defined in the question, doingint features = FEATURE_A | FEATURE_B;
will be compiled asint features = 1 | 2;
which is exactly what the OP seems to want.FEATURE_AC = FEATURE_A | FEATURE_C,
in your enum list...