I believe enumeration types are much better for bit manipulations:
enum SomeOptions {
None = 0,
FirstOption = 1 << 0,
SecondOption = 1 << 1,
ThirdOption = 1 << 2,
}
SomeOptions options;
options |= Options.FirstOption;
options ^= Options.ThirdOption;
options = Options.SecondOption | Options.FirstOption;
bool isFirstOptionSet = (options & Options.FirstOption) > 0;
For some more advanced techniques using enumeration types, please see my article:
Enumeration Types do not Enumerate! Working around .NET and Language Limitations[
^].
You can also combine these bit-oriented techniques with byte (or any other numeric type) access. You can also use the attribute
[FieldOffset]
(
System.Runtime.InteropServices.FieldOffsetAttribute
) to create different fields aligned to the same address and create effects of C++ union. Please see:
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.fieldoffsetattribute.aspx[
^].
Good luck,
—SA