Reputation: 153
We have a database where one of the columns contains composite enum integer values. We need to strip all these values (if they have it) of a specific enum integer value, but leave the rest in place.
[Flags]
public enum MyEnum
{
Enum1 = 1,
Enum2 = 2,
Enum3 = 4,
Enum4 = 8,
}
In the database we find
Row MyEnumSettings
1 3 (Enum1 | Enum2)
2 8 (Enum4)
3 6 (Enum2 | Enum3)
4 14 (Enum2 | Enum3 | Enum4)
We want to remove Enum3 from every row and end up with
Row MyEnumSettings
1 3 (no change)
2 8 (no chnage)
3 2 (removed Enum3)
4 10 (removed Enum3)
We can use Enum.IsDefined to check if an integer is part of an enum (not sure if it works on composite integer values..). But, how do we check if a given integer value contains a specific part of an enum?
Upvotes: 1
Views: 121
Reputation: 13925
This should remove the enum if they are bitwisely added:
Value &= ~Enum3
Basically you need to bitwisely negate the Enum3 which leads to a negative mask, and apply bitwise AND to exclude the bit which is 0.
The opposite operation is, if you need to add it back
Value |= Enum3
Upvotes: 7