tags:

views:

251

answers:

2

What does the CreateMask() function of BitVecto32 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?

+1  A: 

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

You might want to check out wiki what mask is.

In short - mask is a pattern in form of array of ones for those bits what you are interested in and zeros for others.

If you got something like 01010 and you are interested to get last 3 bits, your mask would look like 00111. Then - when you perform OR bitwise operation on 01010 and 00111 you will get last three bits => 00010.

An example might be easier to understand:

BitVector32.CreateMask() returns 1 (in binary => 1)
BitVector32.CreateMask(1) returns 2 (in binary => 10)
BitVector32.CreateMask(2) returns 4 (in binary => 100)
BitVector32.CreateMask(4) returns 8 (in binary => 1000)

CreateMask(int) returns given number multiplied by 2.

P.s. First bit in binary number is first digit from right.

Arnis L.
How is the mask created? eg how does myBit3 specify the 3rd bit?
Sunder
A: 

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

dan