c# - BitVecttor and its use

BitVector is something that provided by the .net Framework, which is provided to aim the development in the memory effecient way.

 

 

You can use the BitVector to create some bit flags construct; The BitVector also has the concept of Sectoin, whereas you can group several bit to a bit section, and operate on the bitsection as a whole.

 

 

The original code is copied and remodeld from the MSDN discussion, for more detail son the APIs, please see the origial site here: "BitVector Structure"

 

 

Use the BitVector to create bit flags

 

In the following code, it shows how to create the bit flags and the relevante bit mask with the help of BItVector.

 

public static void DemoBitVector32()
    {
      BitVector32 myBV = new BitVector32();


      int myBit1 = BitVector32.CreateMask(); // A mask that isolates the bit flag following the one that previous points to in System.Collections.Specialized.BitVector32.
      int myBit2 = BitVector32.CreateMask(myBit1); // it can accept the previous bit order 
      int myBit3 = BitVector32.CreateMask(myBit2); // it can accept the previous bit order 
      int myBit4 = BitVector32.CreateMask(myBit3); // it can accept the previous bit order 
      int myBit5 = BitVector32.CreateMask(myBit4); // it can accept the previous bit order 

      // set the alternating bit to TRUE:
      Console.WriteLine("Setting alternating bits to TRUE");
      Console.WriteLine("  Initial:    {0}", myBV.ToString());

      myBV[myBit1] = true;
      Console.WriteLine("Setting alternating bits to TRUE");
      Console.WriteLine("  Initial:    {0}", myBV.ToString());

      myBV[myBit3] = true;
      Console.WriteLine("Setting alternating bits to TRUE");
      Console.WriteLine("  Initial:    {0}", myBV.ToString());

      myBV[myBit5] = true;
      Console.WriteLine("Setting alternating bits to TRUE");
      Console.WriteLine("  Initial:    {0}", myBV.ToString());

    }
 

Use BitVector to create Bit Sections

 

The following code uses the BitVector to create bit Sections. 

 

public static void DemoBitVector32_v2()
    {
      BitVector32 myBV = new BitVector32(0);

      BitVector32.Section mySect1 = BitVector32.CreateSection(6);  
      BitVector32.Section mySect2 = BitVector32.CreateSection(3, mySect1);
      BitVector32.Section mySect3 = BitVector32.CreateSection(1, mySect2);
      BitVector32.Section mySect4 = BitVector32.CreateSection(15, mySect3);
      
      // Displays the values of the sections.
      Console.WriteLine("Initial values:");
      Console.WriteLine("\tmySect1: {0}", myBV[mySect1]);
      Console.WriteLine("\tmySect2: {0}", myBV[mySect2]);
      Console.WriteLine("\tmySect3: {0}", myBV[mySect3]);
      Console.WriteLine("\tmySect4: {0}", myBV[mySect4]);


      Console.WriteLine("Changing the value of each section\n");
      Console.WriteLine("\tInitial:   \t{0}", myBV.ToString());

      myBV[mySect1] = 5;
      Console.WriteLine("\tmySect1 = 5:\t{0}", myBV.ToString());

      myBV[mySect2] = 3;
      Console.WriteLine("\tmySect1 = 5:\t{0}", myBV.ToString());

      myBV[mySect3] = 1;
      Console.WriteLine("\tmySect1 = 5:\t{0}", myBV.ToString());

      myBV[mySect4] = 9;
      Console.WriteLine("\tmySect1 = 5:\t{0}", myBV.ToString());

      Console.WriteLine("New values:");
      Console.WriteLine("\tmySect1: {0}", myBV[mySect1]);
      Console.WriteLine("\tmySect2: {0}", myBV[mySect2]);
      Console.WriteLine("\tmySect3: {0}", myBV[mySect3]);
      Console.WriteLine("\tmySect4: {0}", myBV[mySect4]);
    }
 

 

 

你可能感兴趣的:(C#)