using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace Hash { class Program { static void Main(string[] args) { byte a = 5; BitArray myBit1 = new BitArray(a);//5个字位,初始化为false //myBit1为5位,取用默认初始化,5位都为 False。即为0,由此可见,平时一位的值0和1在C#里面变成False和True; myBit1[0] = true; myBit1[1] = true; Console.Write("my Bit1 Count:{0},length:{1},值如下:\n", myBit1.Count, myBit1.Length); PrintValues(myBit1, 8);//每8个元素为一行打印元素 byte [] myByte1 = new byte[] { 1, 2, 3, 4, 5 };//字节数组,byte为0-255的类型 BitArray myBit2 = new BitArray(myByte1); //使用myByte1初始化myBit2;共有5*8个字节位; //myByte2为byte数组,内容为1,2,3,4,5;二进制就是00000001,00000010,00000011,00000100,00000101,myBA3就为相应的5*8=40位 //在myByte2中数字按照二进制数从右到左存取 Console.Write("my Bit2 Count:{0},length:{1},值如下:\n", myBit2.Count, myBit2.Length); PrintValues(myBit2, 8);//每8个元素为一行打印元素 bool[] myBools = new bool[5] { true, false, true, true, false }; BitArray myBA4 = new BitArray(myBools); //看输出,bool就想当于一位,myBools是长度为5的数组,变成myBA4后,也是5位; Console.Write("myBA4 Count:{0},length:{1},值如下:\n", myBA4.Count, myBA4.Length); PrintValues(myBA4, 8);//每8个元素为一行打印元素 int[] myInts = new int[5] { 6, 7, 8, 9, 10 }; BitArray myBA5 = new BitArray( myInts ); //int是32位的,5个,换成BitArray当然就是5*32=160。 Console.Write("myBA5 Count:{0},length:{1},值如下:\n", myBA5.Count, myBA5.Length); PrintValues(myBA5, 8);//每8个元素为一行打印元素 Console.ReadKey(); } public static void PrintValues(IEnumerable myList, int myWidth) //myWidth指定每行显示的个数 { int i = myWidth; foreach (Object obj in myList) //迭代一列数 { if (i <= 0) { i = myWidth; Console.WriteLine(); } i--; Console.Write("{0,7}", obj);//显示第0个数据obj,占7个符号的位置 } Console.WriteLine(); } } }