1
我有以下枚舉:標誌數是兩個變量枚舉
[Flags]
public enum Letter
{
NONE = 0,
A = 1,
B = 2,
C = 4,
A_B = A | B,
A_C = A | C,
B_C = B | C,
ALL = A | B | C
}
和我有下面這段代碼:
Letter first = Letter.A_B;
Letter second = Letter.B_C;
如何獲得標誌的數量是在first
變量,而且在second
變量?
我想有結果:
Letter first = Letter.A_B;
Letter second = Letter.B_C;
int numberOfSameFlags = ...; // should return 1 in this example
Letter first = Letter.A_B;
Letter second = Letter.ALL;
int numberOfSameFlags = ...; // should return 2 in this example
我試着位操作,但我不認爲我能得到這個值。
如果你想使用*僅*位運算,你可以做到這一點[這](https://stackoverflow.com/questions/3815165/how實現位數僅使用位運算符)的方式。 –