I am trying to find out if one bitmask has at least one of the same bits set as in another bitmask. I couldn't seem to find a bitwise operator that does this, so I came up with this code that works, though I can't say I really like it.
[Flags]
enum TestFlags { a = 1, b = 2, c = 4, d = 8 }
static void Main(string[] args)
{
TestFlags
bm1 = TestFlags.a | TestFlags.d,
bm2 = TestFlags.b | TestFlags.c | TestFlags.d;
var SetFlags = Enum.GetValues(typeof(TestFlags))
.Cast<TestFlags>()
.Where(v => bm1.HasFlag(v));
foreach (var e in SetFlags.Where(f => bm2.HasFlag(f)))
{
Console.WriteLine(e.ToString());
}
}
Is there any more elegant way to perform this check?