C#位数组
一、引言
如果我们着重处理一个以位为单位的数据时,就可以考虑使用位数组。例如在很多PCI的IO卡中涉及很多的I位的读取以此判断各个开关量的状态,或者设置继电器输出状态时,就会频繁用到位的操作。
这种情况C#提供的BitArray类和BitVectro32类就会起到很大的作用。本篇文章先讲解BitArray类。
二、BitArray类基础
1、BitArray类在命名空间System.Collections中可以找到。
2、BitArray类可以重新设置大小,BitVector32不会,它是固定32位的。
3、BitArray类是一个引用类型,它包含一个int数组,其中每32位使用一个
新整数,所以它的长度是可变的。例如如果BitArray存储的是63位的数 据,就会被存在两个int数据组成的数组中。因为63=32(int)+31(int)其中第二个int只使用了31位。如果BitArray存储的是65位的数据,就会被存在三个int数据组成的数组中,其中第三个int只使用了其中的1位,因为65=32(int)+32(int)+1(int)。
三、BitArray类常用方法
public sealed class BitArray : ICollection, IEnumerable, ICloneable
{
public BitArray(BitArray bits); //用已有的BitArray给新的BitArray初始化
public BitArray(bool[] values); //用布尔数组初始化
public BitArray(byte[] bytes); //用字节数组初始化
public BitArray(int length); //初始化并设置位数值,此值会在使用中自动增长
public BitArray(int[] values); //用int数组初始化
public BitArray(int length, bool defaultValue); //初始化并设置默认值
public int Count { get; } //位数组中现存的位的个数
public bool IsReadOnly { get; } //确定位数组是否只读
public bool IsSynchronized { get; } //是否同步对此BitArray的操作,用在线程安全上
public int Length { get; set; } //位数组的位数
public object SyncRoot { get; }
public bool this[int index] { get; set; } //索引器,利用索引读位值
public BitArray And(BitArray value); //按位与
public object Clone(); //创建BitArray 的浅表副本。
public void CopyTo(Array array, int index); //将BitArray拷贝到其他数组中
public bool Get(int index); //按下标读取位值
public IEnumerator GetEnumerator(); //返回循环访问BitArray 的枚举数
public BitArray Not(); //按位非
public BitArray Or(BitArray value); //按位或
public void Set(int index, bool value); //按位设置值
public void SetAll(bool value); //设置所有位为指定值
public BitArray Xor(BitArray value); //按位异或
}
四、BitArray实例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Collections;
using System.Diagnostics;
using System.Collections.Specialized;
namespace TESTT
{
class Program
{
static void DisplayBit(string tag,BitArray bits) //将位数组输出
{
if(tag!="")
Console.Write(tag+":");
foreach (bool b in bits)
{
Console.Write(b?1:0);
}
Console.WriteLine();
}
static void Main(string[] args)
{
var bits = new BitArray(8); //创建一个8位的位数组
//设置全部位
bits.SetAll(true);
DisplayBit("设置全部为true后",bits);
//设置指定位
bits.Set(1,false);
DisplayBit("设置下标1为false后",bits);
//按索引器设置
bits[5] = false;
DisplayBit("设置下标5为false后", bits);
//Not取反运算
bits.Not();
DisplayBit("bits取反后", bits);
//更改位数组的长度
bits.Length = 16;
bits[9] = true;
DisplayBit("设置下标9为false后", bits);
//新建
var bits2 = new BitArray(bits);
bits2[0] = true;
bits2[2] = true;
bits2[4] = true;
bits2[6] = true;
bits2[8] = true;
bits2[10] = true;
bits2[12] = true;
//Or或运算
DisplayBit("", bits);
Console.Write(" or");
DisplayBit("", bits2);
Console.Write("=");
bits.Or(bits2);
DisplayBit("", bits);
//And运算
DisplayBit("", bits);
Console.Write(" and");
DisplayBit("", bits2);
Console.Write("=");
bits.And(bits2);
DisplayBit("", bits);
//Xor运算
DisplayBit("", bits);
Console.Write(" xor");
DisplayBit("", bits2);
Console.Write("=");
bits.Xor(bits2);
DisplayBit("", bits);
Console.ReadKey();
}
}
}