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]);
    }
 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值