2017-10-13 92 views
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 

我试着位操作,但我不认为我能得到这个值。

+0

如果你想使用*仅*位运算,你可以做到这一点[这](https://stackoverflow.com/questions/3815165/how实现位数仅使用位运算符)的方式。 –

回答

3

您可以将这些标志与AND相加,然后计算设置位的数量(这称为"Hamming Weight"的整数)。你可以指望设置位

的一种方式(有很多,这是一个我一把抓过网):

public static int HammingWeight(int i) 
{ 
    i = i - ((i >> 1) & 0x55555555); 
    i = (i & 0x33333333) + ((i >> 2) & 0x33333333); 
    return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24; 
} 

因此,对于你的问题:

Letter first = Letter.A_B; 
Letter second = Letter.B_C; 
Console.WriteLine(HammingWeight((int)first & (int)second)); 

和:

Letter first = Letter.A_B; 
Letter second = Letter.ALL; 
Console.WriteLine(HammingWeight((int)first & (int)second)); 

如果您想知道特定的实现如何工作,请参阅see here

2

另一种可能的答案是通过BitArray类

int f = Convert.ToInt32(first); 
int s = Convert.ToInt32(second); 
BitArray bit = new BitArray(System.BitConverter.GetBytes(f & s)); 
Console.WriteLine(bit.Cast<bool>().Count(x => x));