Normally, when I create an enum they each get incremented by one, see
enum
{
A = 0,
B,
C,
D
};
but after I looked into some source codes, I see people doing things like this
enum
{
A = 0,
B = 1 << 0,
C = 1 << 1,
D = 1 << 2
};
I understand what it means, but what exactly does this grant me? Are there any advantages? I only see that this makes it look unneccesary complex in my opinion.
The second form creates flags for use in a bitmask. It's normally done to save space in objects that have several boolean conditions that control their behavior.
struct foo {
std::uint32_t bitmask; // up to 32 different flags.
};
foo obj;
obj.bitmask = (B | D); // Sets the bits 0 and 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With