Use Enumerated Values with Bit Flags to Handle Multiple Options

added by Paul Wheeler
5/28/2013 7:14:09 PM

7 Kicks, 149 Views

Some applications need a set of options that can be combined in a variety of ways. In that scenario, a favorite developers' trick is to have each bit in a Byte represent one of the options. By using values that are powers of 2 and logical Ors, a developer can set individual bits in the Byte; using those same values against the Byte, along with logical Ands, a developer can check which bits in a Byte are set. Bringing in enumerated values makes working with bit flags easier to both read and write. **Be sure to read the article comments for some real gems**