3

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?

Dmitry Pavlov
  • 30,789
  • 8
  • 97
  • 121
Sunder
  • 1,487
  • 2
  • 13
  • 11

4 Answers4

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.

Michael
  • 8,362
  • 6
  • 61
  • 88
Arnis Lapsa
  • 45,880
  • 29
  • 115
  • 195
1

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}

Community
  • 1
  • 1
0

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.

Matthijs Wessels
  • 6,530
  • 8
  • 60
  • 103
0

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).

Community
  • 1
  • 1
Daniel
  • 1,195
  • 9
  • 13