c#中的字节数组bitArray

//支持七个 构造函数,可迭代,支持索引器设置访问值
            BitArray b = new BitArray(8); //初始化八个bit位 最多32位?
            //看下默认值,默认值为bool型
            foreach (bool t in b) {
                Console.WriteLine(t); //全部为false ,同样设置默认值得构造
            }
            BitArray d = new BitArray(8, true); //默认为true 就是11111111
            //索引器 访问
            Console.WriteLine(d[0]); // true
            Console.WriteLine(d[1]); // true
            d.Get(0); 
            d.Set(1, false);
            d.SetAll(true); //全部设置为 true
            /*
            d.And(); //位运算 相当于 &
            d.Xor(); //异或
            d.Not(); //取反
            d.Or(); //位或 */
            int len = 8; //需要取出的位数
            byte[] buf = new byte[len]; //缓冲区
            d.CopyTo(buf, 0); //复制 8 个bit
            Console.WriteLine(BitConverter.ToInt32(buf, 0)); // 255;
            Console.ReadKey(true);

 

posted @ 2017-12-15 10:23  我是外婆  阅读(1743)  评论(0编辑  收藏  举报