97 points · by nice_byte · 8y ago · 40 comments
> return 1 << static_cast<underlying_type>(o);
Relying on default enum values to and automagically converting these into power of two bit masks is very brittle, and can create unexpected bugs and interoperability problems. The mask values change if the enum changes, unused bits in the middle of the bitmask are hard to handle, and the bit order is assumed with the first item being the LSB. The last point is particularly relevant given the NES buttons example, because it uses a bit order opposite to how the controller data is usually read[1]. This article defines
#define BTN_A 0x01 /* 00000001 in binary */
#define BTN_B 0x02 /* 00000010 in binary */
/* ... */
#define BTN_RIGHT 0x80 /* 10000000 */
The standard controller[2] state is sent from a shift register in the controller one bit at a time. This is usually read by the program as 8 reads from the I/O port, and shifted into a button state bitmask. E.g. something like: n = 8;
buttons = 0;
while(buttons--) {
buttons = buttons << 1;
buttons |= (CONTROLLER_1_IO_PORT & 0x01);
}
With the buttons being read in the order [A, B, ..., RIGHT], the recommended bitmask is: BUTTON_A = 1 << 7
BUTTON_B = 1 << 6
/* ... */
BUTTON_RIGHT = 1 << 0
Obviously this is a minor mistake in a (probably untested) example. I just want to highlight how easily this type of "clever" - but brittle - coding style can introduce errors. Defining bit values (and other externally defined magic numbers) explicitly in e.g. a defined-to-be-canonical header file (which might be just those 8 #defines) is much safer and easier to debug/maintain. Then wrap those explicitly defined values into the C++ template for type safety or other fancy features.But I agree with your point.
while (n--)I'm all for modern C++, but this is correct, at least IME with embedded systems. You're overall better off sticking with C-style if working at an abstraction level where the interface itself uses bitmasks. At this point you're manipulating memory manually which is (a) C's wheelhouse and (b) where type safety becomes largely irrelevant.
There is a lot to be said for using custom enum class implementations (e.g. http://www.drdobbs.com/when-enum-just-isnt-enough-enumeratio...) but it isn't clear to me how this kind of thing benefits bitmasks when the goal is presumably absolute maximum efficiency.
Personally though, I wouldn't use this safety in most code. You'd have to be in some pretty serious shit for these templates to reduce mental overload rather than add it.
Plus, with a small amount extra code you can do things like iterate over all the flags that are set etc.
enum {
FOO_A,
FOO_B,
FOO_C,
NUM_FOOS,
};
struct FooInfo {
int kind; // FOO_??
const char *str;
const char *long;
};
static const struct FooInfo fooInfo[NUM_FOOS] = {
{ FOO_A, "FOO_A", "The A of all the foos" },
{ FOO_B, "FOO_B", "The B of all the foos" },
{ FOO_C, "FOO_C", "The C of all the foos" },
};
for (int i = 0; i < NUM_FOOS; i++) {
printf("Got #%d, %s. Description: %s\n",
fooInfo[i].kind, fooInfo[i].str, fooInfo[i].long);
}
More goodies: // if you want to use them as bitmasks
// (but only few enums are used this way)
enum {
FOO_A_BIT = 1 << FOO_A,
FOO_B_BIT = 1 << FOO_B,
FOO_C_BIT = 1 << FOO_C,
};
// if you want to make strings out of C symbols, you
// can also use a macro like this:
#define FOOINFO(name, long) { name, #name, long },
static const struct FooInfo fooInfo[NUM_FOOS] = {
FOOINFO( FOO_A, "The A of all the foos" ),
FOOINFO( FOO_B, "The B of all the foos" ),
FOOINFO( FOO_C, "The C of all the foos" ),
};You can avoid that by using the X-Macro pattern, but that's quite heavy-handed and tedious to do if you have a lot of different enums.
It also doesn't use "enum class" which means that the namespace will be polluted unnecessarily.
That is, given:
enum class Button {
A, B, Select, Start, Up, Down, Right, Left
};
There is no way to define: bitmask<Button> controller_state { Button::A | Button::B };
And then to find out if either of these buttons are pressed. The only way to do it is: bool fn2(bitmask<Button> controller_state)
{
return controller_state & Button::A || controller_state & Button::B;
}
which _does_ generate the optimal code for checking if either is set. However you can't do something like void handleDirectionButtons(bitmask<Button> controller_state);
and then call that with something like: bitmask<Button> direction_buttons { Button::Up | Button::Down | Button::Right | Button::Left };
handleDirectionButtons(state & directionButtons)
Of course, this could be added, see https://godbolt.org/g/48asyeYou could also do something similar for the ~ case as well, however that would end up looking a bit uglier, as you would need to create more bit masks, something like:
~bitmask(Button::Up) & ~bitmask(Button::Down)
For an example, see https://godbolt.org/g/wN2xUV struct gamecontroller {
bool up: 1;
bool down: 1;
bool left: 1;
bool right: 1;
bool a: 1;
bool b: 1;
bool x: 1;
bool y: 1;
};
That's a structure with a size of one byte. Bit fields work just fine in C and C++, yet many programmers don't know this.Microsoft Specific
The ordering of data declared as bit fields is from low to high bit.
END Microsoft Specific[1]
In practice, everybody does it that way.
For fields only one byte wide, this isn't an issue for major compilers. NTP has been using this for decades without problems. But for structs more than one byte wide, there are little-endian/big endian problems. Just like with the numeric types.
This is only an issue if you intend to write the bitfield to a network, file, or device. Locally, you usually don't care about the representation.
The network wire doesn't need a C++ ABI-compatible object, and clearly isn't portable when communicating between systems running different software.
For transmitting data over network, convert from local format to network format in transit.
> can only be used to save a bytes of memory
Yes, that's what "packing" is for.
"I think enums allow a bit more flexibility. For example, if you need to store a piece of data that indicates which option to set, it's easier to do with an enum (can't store the "name" of the bitfield, and you're not permitted to take its address or offset either IIRC). I also like the 'Option1 | Option2 | ... ' syntax for creating bitmasks on the fly (not sure how you'd achieve that with bitfields)"
You can, of course, do something similar with member functions, e.g.
class controller
{
unsigned value;
static constexpr unsigned BTN_A = 0x01;
...
public:
bool a() const { return value & BTN_A; }
void a(bool set) { if (set) value |= BTN_A; else value &= ~BTN_A; }
};But in my opinion it violates principle of least surprise: when I encounter any enum, am I able to OR two values? These do not seem like flags, is this correct usage?
When a function expects bitmask<T>, I should know that T is just flag positions. But when I do not have some API point, only enum, how am I supposed to know that?
Plus, in case of interop with other languages, even with C, I will have Button::B == 2 in CPP, and BUTTON_B == 4 in C (or Button.B == 4 in Java). This of course adds to confusion...
Other than that, nice article.
"The non-member function operator overloads that return bitmask<option_type> will not be considered for overload resolution if the enums are in a different namespace than the bitmask definition. The template deduction of operator| would not be sufficient since it does not trigger ADL from outside the namespace -- the overload must be available from within the scope where it used. This effectively requires a using directive or declaration to make them visible to participate in resolution (and at which, this will attempt to participate in resolution of any enum type where it may be considered a better match)."
Is this correct? Because if it is, C++ is bonkers...
I personally dislike overloading very much so I'm probably not the best person to defend it though.
-
uint8_t controller_state = BTN_A | BTN_B;
if I read correctly this example would not be possible anymore right? bitmask<Button> controller_state = Button::A | Button::B;
or bitmask<Button> controller_state { Button::A | Button::B };
won't work.For example instead of
#define BTN_A 0x01
you have bitmask<Button>(A)
Both of these are known at compile time and they are equivalent.