c# - BitVector32的CreateMask函数有什么作用?

标签 c# .net bitvector

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/

相关文章:

c# - 使用 Windows 服务和 Vb.Net 检测 USB 驱动器的插入和移除

c# - 如何在 C# 中将 lambda 表达式作为参数传递

c# - 理解有漏洞的应用程序的 Performance Profiler 输出?

java - 确定一个字符串具有所有唯一字符而不使用额外的数据结构并且没有小写字符假设

performance - 在 Haskell 中使用高效异或和位计数打包大位向量

c# - 在 WPF 中获取数据网格中的多个选定行?

c# - 无法使用 Gtk Builder 加载 glade 文件。 Gtk3#

.net - 需要创建一个也保存类型的动态 ConfigurationSection

c# - 不同语言的注册表值

integer - 尝试用 VHDL 模拟电路时出现 "range constraint violation"错误