When using the [Flags]
attribute you should explicitly map the enum values to integer that contain non overlapping bit patterns. That is, each enum value should be mapped to a power of two:
[Flags]
enum Editions
{
Educational = 1,
Basic = 2,
Pro = 4,
Ultra = 8
}
Without the explicit numbering, Educational
will be mapped to 0 and Basic
to 1.
Enum.HasFlags
checks if the bit field or bit fields that are set in the parameter are also all set in the tested enum. In your case, x
is 1 and y
is 0. That means that x
contains all of the bits set in 0
(that is no bits at all). However 0
does not contain the bits set in 1
when testing the other way around.