• BitArray.cs


    /******************************************************************************
    Module:  BitArray.cs
    Notices: Copyright (c) 2002 Jeffrey Richter
    ******************************************************************************/


    using System;


    ///////////////////////////////////////////////////////////////////////////////


    public sealed class BitArray {
       // Private array of bytes that hold the bits
       private Byte[] byteArray;
       private Int32 numBits;

       // Constructor that allocates the byte array and sets all bits to 0
       public BitArray(Int32 numBits) {
          // Validate arguments first.
          if (numBits <= 0)
             throw new ArgumentOutOfRangeException("numBits must be > 0");

          // Save the number of bits.
          this.numBits = numBits;

          // Allocate the bytes for the bit array.
          byteArray = new Byte[(numBits + 7) / 8];
       }


       // This is the indexer.
       public Boolean this[Int32 bitPos] {
          // This is the index property抯 get accessor method.
          get {
             // Validate arguments first
             if ((bitPos < 0) || (bitPos >= numBits))
                throw new IndexOutOfRangeException();

             // Return the state of the indexed bit.
             return((byteArray[bitPos / 8] & (1 << (bitPos % 8))) != 0);
          }

          // This is the index property抯 set accessor method.
          set {
             if ((bitPos < 0) || (bitPos >= numBits))
                throw new IndexOutOfRangeException();
             if (value) {
                // Turn the indexed bit on.
                byteArray[bitPos / 8] = (Byte)
                   (byteArray[bitPos / 8] | (1 << (bitPos % 8)));
             } else {
                // Turn the indexed bit off.
                byteArray[bitPos / 8] = (Byte)
                   (byteArray[bitPos / 8] & ~(1 << (bitPos % 8)));
             }
          }
       }
    }


    ///////////////////////////////////////////////////////////////////////////////


    class App {
       static void Main() {
          // Allocate a BitArray that can hold 14 bits.
          BitArray ba = new BitArray(14);

          // Turn all the even-numbered bits on by calling the set accessor.
          for (Int32 x = 0; x < 14; x++) {
             ba[x] = (x % 2 == 0);
          }

          // Show the state of all the bits by calling the get accessor.
          for (Int32 x = 0; x < 14; x++) {
             Console.WriteLine("Bit " + x + " is " + (ba[x] ? "On" : "Off"));
          }
       }
    }


    //////////////////////////////// End of File //////////////////////////////////
  • 相关阅读:
    appium 命令安装记录
    用命令方式启动、停止appium服务和app
    adb连接网易mumu模拟器
    获取APP包名,launcherActivity
    jekins配置
    Appium工作原理
    关于An association from the table XX refers to an unmapped class错误
    什么是MYSQL回表查询
    MySQL的in和or的效率问题
    MYSQL语句优化
  • 原文地址:https://www.cnblogs.com/shihao/p/2501249.html
Copyright © 2020-2023  润新知