BitVector32的CreateMask函数是做什么的?

发布于 2024-08-04 17:20:38 字数 1295 浏览 7 评论 0 原文

CreateMask() BitVector32 做什么? 我不明白什么是面具。

想了解以下几行代码。创建掩码是否只是将位设置为 true?

  // 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() );

这有什么实际应用呢?

What does the CreateMask() function of BitVector32 do?
I did not get what a Mask is.

Would like to understand the following lines of code. Does create mask just sets bit to true?

  // 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() );

What is the practical application of this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

顾铮苏瑾 2024-08-11 17:20:38

它返回一个掩码,您可以使用它来更轻松地检索感兴趣的位。

您可能需要查看维基百科了解掩码是什么。

简而言之:掩码是一种模式,其形式为您感兴趣的位为 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。

注意:第一位是最低有效位,即最远的位。右边的。

It returns a mask which you can use for easier retrieving of interesting bit.

You might want to check out Wikipedia for what a mask is.

In short: a mask is a pattern in form of array of 1s for the bits that you are interested in and 0s for the others.

If you have something like 01010 and you are interested in getting the last 3 bits, your mask would look like 00111. Then, when you perform a bitwise AND on 01010 and 00111 you will get the last three bits (00010), since AND only is 1 if both bits are set, and none of the bits beside the first three are set in the mask.

An example might be easier to understand:

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) returns the given number multiplied by 2.

NOTE: The first bit is the least significant bit, i.e. the bit farthest to the right.

鸩远一方 2024-08-11 17:20:38

BitVector32.CreateMask() 是左移运算符 (<<) 的替代,在大多数情况下会导致乘以 2(左移不是循环,因此您可能会开始丢失数字,更多此处进行了解释

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{00000000000000000000000000000000}
BitVector32{00000000000000000000000000010111}

BitVector32.CreateMask() is a substitution for the left shift operator (<<) which in most cases results in multiplication by 2 (left shift is not circular, so you may start loosing digits, more is explained 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());

Output:

BitVector32{00000000000000000000000000000000}
BitVector32{00000000000000000000000000010111}

半暖夏伤 2024-08-11 17:20:38

查看另一篇文章链接文本
而且,CreateMask 不会返回给定数字乘以 2。
CreateMask 根据 32 位字中的特定位置(即您要传递的参数)创建位掩码,当您谈论单个位(标志)时,通常为 x^2。

Check this other post link text.
And also, CreateMask does not return the given number multiplied by 2.
CreateMask creates a bit-mask based on an specific position in the 32-bit word (that's the paramater that you are passing), which is generally x^2 when you are talking about a single bit (flag).

暖心男生 2024-08-11 17:20:38

我偶然发现了这个问题,试图找出 CreateMask 到底做了什么。我觉得目前的答案没有回答我的问题。经过一些阅读和实验,我想分享我的发现:

基本上 Maksymilian 所说的几乎是正确的:“BitVector32.CreateMask 是左移运算符 (<<) 的替代,在大多数情况下会导致乘以 2” 。

因为<<是一个二元运算符,而 CreateMask 仅接受一个参数,我想补充一点,BitVector32.CreateMask(x) 相当于 x << 1..

然而

BitVector32.CreateMask(x) 并不等于 x << 1 对于两种边界情况:

  1. BitVector32.CreateMask(int.MinValue):
    将抛出 InvalidOperationException。 int.MinValue 对应于 10000000000000000000000000000000。这似乎有点奇怪。特别是考虑到最左边的位为 1(即负数)的所有其他值都可以正常工作。相反:int.MinValue << 1 不会抛出异常,只会返回 0。

  2. 当您调用 BitVector32.CreateMask(0)(或 BitVector32.CreateMask())时。这将返回 1
    (即 00000000000000000000000000000000 变为 00000000000000000000000000000001),
    而 0 << 1 只会返回 0。

乘以 2

CreateMask 几乎总是等价于乘以 2。除了上述两种特殊情况外,当左数第二位与最左位不同时,情况会有所不同。 int 有符号,因此最左边的位表示符号。在这种情况下,标志就会翻转。例如,CreateMask(-1)(或111111111111111111111111111111111)结果为-2(或11111111111111111111111111111110),但CreateMask(int.MaxValue) (或01111111111111111111111111111111)也会导致-2。

无论如何,您可能不应该将其用于此目的。据我了解,当您使用 BitVector32 时,您实际上应该只将其视为 32 位序列。他们将整数与 BitVector32 结合使用的事实可能只是因为它很方便。

CreateMask 什么时候有用?

老实说我不知道​​。从 文档 和函数参数的名称“previous”看来他们打算以某种顺序使用它:“使用 CreateMask() 创建系列中的第一个掩码,并使用 CreateMask(int) 创建所有后续掩码。”。

然而,在代码示例中,他们使用它来创建前 5 位的掩码,以便随后对这些位进行一些操作。我无法想象他们期望您连续向 CreateMask 写入 32 个调用,以便能够对左侧附近的位执行一些操作。

I stumbled upon this question trying to find out what CreateMask does exactly. I did not feel the current answers answered the question for me. After some reading and experimenting, I would like to share my findings:

Basically what Maksymilian says is almast correct: "BitVector32.CreateMask is a substitution for the left shift operator (<<) which in most cases results in multiplication by 2".

Because << is a binary operator and CreateMask only takes one argument, I would like to add that BitVector32.CreateMask(x) is equivalant to x << 1.

Bordercases

However, BitVector32.CreateMask(x) is not equivalant to x << 1 for two border cases:

  1. BitVector32.CreateMask(int.MinValue):
    An InvalidOperationException will be thrown. int.MinValue corresponds to 10000000000000000000000000000000. This seems bit odd. Especially considering every other value with a 1 as the leftmost bit (i.e. negative numbers) works fine. In contrast: int.MinValue << 1 would not throw an exception and just return 0.

  2. When you call BitVector32.CreateMask(0) (or BitVector32.CreateMask()). This will return 1
    (i.e. 00000000000000000000000000000000 becomes 00000000000000000000000000000001),
    whereas 0 << 1 would just return 0.

Multiplication by 2

CreateMask almost always is equivalent to multiplication by 2. Other than the above two special cases, it differs when the second bit from the left is different from the leftmost bit. An int is signed, so the leftmost bit indicates the sign. In that scenario the sign is flipped. E.g. CreateMask(-1) (or 11111111111111111111111111111111) results in -2 (or 11111111111111111111111111111110), but CreateMask(int.MaxValue) (or 01111111111111111111111111111111) also results in -2.

Anyway, you probably shouldn't use it for this purpose. As I understand, when you use a BitVector32, you really should only consider it a sequence of 32 bits. The fact that they use ints in combination with the BitVector32 is probably just because it's convenient.

When is CreateMask useful?

I honestly don't know. It seems from the documentation and the name "previous" of the argument of the function that they intended it to be used in some kind of sequence: "Use CreateMask() to create the first mask in a series and CreateMask(int) for all subsequent masks.".

However, in the code example, they use it to create the masks for the first 5 bits, to subsequently do some operations on those bits. I cannot imagine they expect you to write 32 calls in a row to CreateMask to be able to do some stuff with the bits near the left.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文