Visakh V A
Visakh V A

Reputation: 320

Enum with flag and Bitwise operation

I have a senario and I am using following enum for the same

[Flags]
enum State
{
    None = 0x00,
    Added = 0x01,
    Edited = 0x02,
    Commented = 0x04,
    Uncommented = 0x8,
    Reordered = 0x16
}

I wanted to get result like this

if Added result will be

Added

if then Edited result will be

Added, Edited

if then Reordered will be

Added, Edited, Reordered

if then Commented will be

Added, Edited, Reordered, Commented

if then Uncommented will be

Added, Edited, Reordered, Commented , Uncommented

if then Commented will be

Added, Edited, Reordered, Commented , Uncommented , Commented

and so on.

Please advice whether I can do the same using any Bitwise operation.

Upvotes: 0

Views: 265

Answers (1)

Rotem
Rotem

Reputation: 21917

Two of your requirements make it impossible to use a bitwise flags enum for this purpose.

  • There is no way to represent the order in which bits were set.
  • There is no way to represent a bit being set more that once.

All of these actions are indistinguishable when using a bitwise flag enum:

Added, Edited, Reordered
Added, Reordered, Edited
Added, Edited, Reordered, Edited

They are all represented as Added | Edited | Reordered.

As mentioned in comments, you should instead use a List<State> to represent this data.

Upvotes: 2

Related Questions