What does the CreateMask function do for BitVector32?

What does CreateMask()the BitVector32do function do? I did not understand what the Mask is.

I would like to understand the following lines of code. Does the mask only create bit bits in 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?

+3
source share
4 answers

BitVector32.CreateMask()is a replacement for the left shift operator ( <<), which in most cases leads to multiplication by 2 (on the left the shift is not circular, so you can start playing numbers, 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}

+1
source

, .

.

: 1s 0s .

- 01010, 3 , 00111. , 01010 00111, (00010), 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.

. , , .

+1

. , CreateMask , 2. CreateMask 32- ( ), x ^ 2, ().

0

, , CreateMask. , . :

, , : "BitVector32.CreateMask - (< <), 2".

< , CreateMask , , BitVector32.CreateMask(x) x << 1.

BitVector32.CreateMask(x) x << 1 :

  • BitVector32.CreateMask(int.MinValue):
    InvalidOperationException. int.MinValue 10000000000000000000000000000000. . 1, (.. ) . : int.MinValue << 1 0.

  • BitVector32.CreateMask(0) ( BitVector32.CreateMask()). 1
    (.. 00000000000000000000000000000000 00000000000000000000000000000001),
    0 < 1 0.

2

CreateMask 2. , , . Int , . . . CreateMask(-1) ( 11111111111111111111111111111111) -2 ( 11111111111111111111111111111110), CreateMask(int.MaxValue) ( 01111111111111111111111111111111) -2.

, , , . , BitVector32, 32 . , ints BitVector32, , , .

CreateMask?

, . "previous" , - : " CreateMask() CreateMask (int) .".

5 , . , , 32 CreateMask, .

0

Source: https://habr.com/ru/post/1717723/


All Articles