As Jacob explained in a comment your method is not correct at all. Personally I always avoid programming mathematically, especially when it comes to logic. So my solution would be something like "if I wanted to know the count is one, so count it and compare it to number one".
Here it is:
public static bool OneIsSet(Type enumType, byte value)
{
return Enum.GetValues(enumType).Cast<byte>().Count(v => (value & v) == v) == 1;
}
public static bool OneIsSet(Type enumType, int value)
{
return Enum.GetValues(enumType).Cast<byte>().Count(v => (value & v) == v) == 1;
}
And you can use it for your foo type like this:
var toReturnFalse = (byte)(foo.Flag1 | foo.Flag2);
var toReturnTrue = (byte)foo.Flag1;
var trueWillBeReturned = OneIsSet(typeof(foo), toReturnTrue);
var falseWillBeReturned = OneIsSet(typeof(foo), toReturnFalse);
I believe this methods could be written in a more generic way using Generics and type handling methods. However I included the methods for most common base types for enums which are int and byte. But you could also write the same for short and other types.
Also you may just inline the code in your code. It is only one line of code.
Also using this method you could see if the number of set flags is two or more. The below code returns true if the count of set flags is equal to 'n'.
Enum.GetValues(enumType).Cast<byte>().Count(v => (value & v) == v) == n;