BitVector32的CreateMask()函数是做什么的?我不明白什么是面具。
我想了解以下几行代码。create mask只是将位设置为真吗?
// Creates and initializes a BitVector32 with all bit flags set to FALSE.
BitVector32 myBV = new BitVector32( 0 );
// Creates masks to isolate each of the first five bit flags.
int myBit1 = BitVector32.CreateMask();
int myBit2 = BitVector32.CreateMask( myBit1 );
int myBit3 = BitVector32.CreateMask( myBit2 );
int myBit4 = BitVector32.CreateMask( myBit3 );
int myBit5 = BitVector32.CreateMask( myBit4 );
// Sets the alternating bits to TRUE.
Console.WriteLine( "Setting alternating bits to TRUE:" );
Console.WriteLine( " Initial: {0}", myBV.ToString() );
myBV[myBit1] = true;
Console.WriteLine( " myBit1 = TRUE: {0}", myBV.ToString() );
myBV[myBit3] = true;
Console.WriteLine( " myBit3 = TRUE: {0}", myBV.ToString() );
myBV[myBit5] = true;
Console.WriteLine( " myBit5 = TRUE: {0}", myBV.ToString() );它的实际应用是什么?
发布于 2011-09-28 18:37:51
BitVector32.CreateMask()是左移位运算符(<<)的替代,在大多数情况下会导致乘以2(左移位不是循环的,因此您可能会开始丢失数字,更多信息将在here中解释)
BitVector32 vector = new BitVector32();
int bit1 = BitVector32.CreateMask();
int bit2 = BitVector32.CreateMask(bit1);
int bit3 = 1 << 2;
int bit5 = 1 << 4;
Console.WriteLine(vector.ToString());
vector[bit1 | bit2 | bit3 | bit5] = true;
Console.WriteLine(vector.ToString());输出:
BitVector32{00000000000000000000000000010111}
BitVector32{0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000}
发布于 2009-09-15 11:02:17
它返回一个掩码,您可以使用它来更容易地检索感兴趣的比特。
你可能想通过check out Wikipedia来了解面具是什么。
简而言之:掩码是以1为数组形式的模式,对于感兴趣的位是0。
如果你有类似01010的东西,并且你对最后3位感兴趣,那么你的掩码看起来就像00111。然后,当您在01010和00111上执行逐位AND运算时,您将得到最后三位(00010),因为只有在设置了这两位的情况下,and才是1,并且前三位之外的任何位都没有在掩码中设置。
下面的例子可能更容易理解:
BitVector32.CreateMask() => 1 (binary 1)
BitVector32.CreateMask(1) => 2 (binary 10)
BitVector32.CreateMask(2) => 4 (binary 100)
BitVector32.CreateMask(4) => 8 (binary 1000)CreateMask(int)返回给定的数字乘以2。
注意::第一位是最低有效位,即最右边的位。
发布于 2010-10-21 04:08:08
请查看另一篇文章link text。而且,CreateMask 不会返回乘以2的给定数字。CreateMask基于32位字中的特定位置(这是您传递的参数)创建位掩码,当您讨论单个位(标志)时,位掩码通常是x^2。
https://stackoverflow.com/questions/1426470
复制相似问题