using System;
using System.Collections;
using System.Text;


namespace TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            var bits = new BitArray(4);
            bits.SetAll(true);

            Console.WriteLine(bits.ToInt());
             Console.WriteLine(bits.ToStr());
            
          
            Console.ReadKey();
        }

    }

    public static class BitArryExtention
    {
        /// <summary>
        /// 将BitArray转为整数
        /// </summary>
        /// <param name="bitArray"></param>
        /// <returns></returns>
        public static int ToInt(this BitArray bitArray)
        {
            int j = 0;
            for (int i = 0; i < bitArray.Count; i++)
            {
                j += (bitArray[i] ? 1 : 0)  << i;
            }
            return j;
        }
        public static string ToStr(this BitArray bitArray)
        {
            StringBuilder j = new StringBuilder();
            foreach (bool bit in bitArray)
            {
                j.Append(bit ? 1 : 0);
            }
            return j.ToString();
        }
    }

}

相关文章:

  • 2021-08-08
  • 2021-08-03
  • 2021-11-24
  • 2021-04-09
  • 2022-02-02
  • 2022-03-09
  • 2021-07-03
  • 2021-10-21
猜你喜欢
  • 2021-08-24
  • 2021-11-20
  • 2021-11-20
  • 2021-12-28
  • 2021-07-10
  • 2021-05-17
  • 2021-12-04
相关资源
相似解决方案