The usual reason for doing this is to associate each enum value with a single bit of a final v value, so you can (for one example) encode a number of flags into a single variable.
For example, with a typical 32-bit system, you can encode (obviously enough) 32 individual flags into a single 32-bit int
(or, preferably, unsigned int
).
For example, if you're looking at keys on the keyboard, you could encode "normal" keys like letters and digits into one byte (probably using consecutive values, as you've suggested), and "modifier" keys like shift, alt, and control as individual bits. This will allow you (for example) to encode something like control+alt+A as a single value.
Likewise, for mouse messages you could treat the mouse buttons as "modifiers", so you could encode something like dragging the mouse as a single value.
In both cases, the important point of encoding the "modifiers" as individual bits is that this allows you to later retrieve those modifiers unambiguously -- if the right bit is set in the value, then that modifier was use. By contrast, if you just use consecutive numbers, you can't extract individual pieces afterwards. For example, if you have inputs encoded as 1
, 2
and 3
, you can't tell whether a 3
is intended to indicate an original input corresponding to 3
, or inputs of both 1
and 2
at the same time. If, however, you encode the values as 1
, 2
and 4
, you can combine values, and still decode them, so you can see exactly what input(s) were necessary to produce a particular value.