Normally when comparing flag enums I use the following format:
(value & flag) == flag;
But sometimes I come across this:
(value & flag) != 0;
Just wondering which is the better to use, or does it come down to personal preference?
if you are using .net 4 or higher use Enum.HasFlag instead
In fact this method uses first way of checking, but provide more clear way to check flags
So long as flag
is a one-bit flag, they are equivalent. If flag
has multiple bits,
(value & flag) == flag;
is a logical AND (ALL bits must match) while
(value & flag) != 0;
is a logical OR (ANY of the bits must match).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With