Picture of Using Enumerated Types as Bitflags
Using the individual bits in any sized integer is a common and very useful (that is, frugal and parsimonious) way to add flags to a program. Instead of using Boolean values or integer values with may be anywhere from 8-bits (one byte) to even four bytes (32 bits), you can instead "flip" bits inside an integer used as an atomic storage location. Of course, by atomic, here I am referring to each individual bit making up the integer as its own unique, useable, indivisible, atomic unit.

Often flags are specified in hexadecimal notation using powers of two (to access individual bit locations) and created using notation that defines the meaning of a particular bit location in an unsigned integer flag. There are several problems with this despite its popularity and wide use. The first problem is that once you've defined your flag values, it's nearly impossible to insert new flags except at the end, lest you have to readjust and shift down all trailing values behind the newly inserted one. Then, possibly, the most important problem is that it isn't type-safe. That is to say, the way bit values are typically defined (using the preprocessor macro #define) doesn't allow for any strict type checking at compile time, leaving the check for runtime, which can create and propagate sometimes very subtle errors in your code.

This short instructable will show you a new method of creating flags using the bit values of an integer that not only allows you to insert new flags as they are required without any renumbering, but also provides strong type-checking, as well.
Remove these adsRemove these ads by Signing Up
talonsblade2 years ago
ive done this before for permissions on some reports in our application. if a certain bit was set the user had access to this report, worked great
nevdull (author)  talonsblade2 years ago
ah, very nice. good use of bit flags!