CreateMask()
是什么意思? BitVector32
的功能做?
我不明白面具是什么。
想看懂下面几行代码。创建掩码是否只是将位设置为真?
// 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() );
这有什么实际应用?
最佳答案
它返回一个掩码,您可以使用它来更轻松地检索有趣的位。
您可能想要 check out Wikipedia什么是面具。
简而言之:掩码是您感兴趣的位为 1,其他位为 0 的阵列形式的模式。
如果你有类似 01010 的东西并且你有兴趣获得最后 3 位,你的掩码看起来像 00111。然后,当你对 01010 和 00111 执行按位与时,你将获得最后三位 (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。
注意:第一位是最低有效位,即最右边的位。
关于c# - BitVector32的CreateMask函数有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1426470/