Values in Bit-Coded Enums

Question
What values should you use for items in bit-coded enums?

Answer
Use powers of two for values in bit-coded enums. Never use negative values.

Comments
Avoid negative values because they behave in odd ways when combined with the | (or) operator.

Example

[Flags]
public enum AccessLevel {
    None = 0,
    Read = 1,
    Write = 2,
    ReadWrite = Read | Write
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s