BitVector32 结构只提供 32 位大小的储存空间(同 Int32), 通过它可以快速存取布尔值或小整数(譬如 7 以内或 15 以内的小整数等等).
主要成员:
/* 属性 */
Data; //其 32 位值的整数表示, 只读; 可以通过它持久记录 BitVector32 的状态
/* 静态方法 */
BitVector32.CreateMask(); //建立 Mask
BitVector32.CreateSection(); //建立 BitVector32.Section
BitVector32.ToString(); //返回指定 BitVector32 对象的位状态字符串
/* 方法 */
ToString(); //返回位状态字符串
/* 与之相关的结构 BitVector32.Section 也有两个属性 */
Mask; //位掩码
Offset; //偏移
构建方法:
protected void Button1_Click(object sender, EventArgs e)
{
BitVector32 bv1 = new BitVector32(); //同 new BitVector32(0);
BitVector32 bv2 = new BitVector32(0); //置 32 个位全为 0
BitVector32 bv3 = new BitVector32(0x0000FF0F); //使用一个 int 值的 "位"
BitVector32 bv4 = new BitVector32(bv3); //使用另一个 BitVector32 的数据构建
BitVector32 bv5 = new BitVector32(-1); //这样可全置位
string s1 = bv1.ToString(); //BitVector32{00000000000000000000000000000000}
string s2 = bv2.ToString(); //BitVector32{00000000000000000000000000000000}
string s3 = bv3.ToString(); //BitVector32{00000000000000001111111100001111}
string s4 = bv4.ToString(); //BitVector32{00000000000000001111111100001111}
string s5 = bv5.ToString(); //BitVector32{11111111111111111111111111111111}
TextBox1.Text = s1 + "\n" + s2 + "\n" + s3 + "\n" + s4 + "\n" + s5;
}
数组式访问:
//假如用 BitVector32 储存一个数字的话, 它能储存 UInt32.MaxValue 个.
protected void Button1_Click(object sender, EventArgs e)
{
BitVector32 bv = new BitVector32(0);
bv[1] = true;
string s1 = bv.ToString(); //BitVector32{00000000000000000000000000000001}
bv[-1] = false; bv[2] = true; //全部清空后重新赋值
string s2 = bv.ToString(); //BitVector32{00000000000000000000000000000010}
bv[-1] = false; bv[3] = true; //BitVector32{00000000000000000000000000000011}
string s3 = bv.ToString();
bv[-1] = false; bv[4] = true; //BitVector32{00000000000000000000000000000100}
string s4 = bv.ToString();
bv[-1] = false; bv[5] = true; //BitVector32{00000000000000000000000000000101}
string s5 = bv.ToString();
bv[-1] = false; bv[6] = true; //BitVector32{00000000000000000000000000000110}
string s6 = bv.ToString();
bv[-1] = false; bv[7] = true; //BitVector32{00000000000000000000000000000111}
string s7 = bv.ToString();
bv[-1] = false; bv[Int32.MaxValue] = true; //BitVector32{01111111111111111111111111111111}
string s8 = bv.ToString();
TextBox1.Text = s1 + "\n" + s2 + "\n" + s3 + "\n" + s4 + "\n" + s5 + "\n" + s6 + "\n" + s7 + "\n" + s8;
}
分别访问每一个位:
//这要通过位掩码(Mask)访问
protected void Button1_Click(object sender, EventArgs e)
{
BitVector32 bv = new BitVector32(0);
int n1 = (int)Math.Pow(2, 0);
int n2 = (int)Math.Pow(2, 1);
int n3 = (int)Math.Pow(2, 2);
int n4 = (int)Math.Pow(2, 3);
int n5 = (int)Math.Pow(2, 4);
int n32 = (int)Math.Pow(2, 31);
bv[n3] = true;
string s1 = bv.ToString(); //BitVector32{00000000000000000000000000000100}
bv[n1] = true;
bv[n2] = true;
bv[n3] = true;
bv[n4] = true;
bv[n5] = true;
bv[n32] = true;
string s2 = bv.ToString(); //BitVector32{10000000000000000000000000011111}
bv[-1] = true; //全置位
bv[n1] = false;
bv[n2] = false;
bv[n3] = false;
bv[n4] = false;
bv[n5] = false;
bv[n32] = false;
string s3 = bv.ToString(); //BitVector32{01111111111111111111111111100000}
TextBox1.Text = s1 + "\n" + s2 + "\n" + s3;
}
BitVector32.CreateMask():
//这是设计者提供的访问 BitVector32 位的方式
protected void Button1_Click(object sender, EventArgs e)
{
BitVector32 bv = new BitVector32(0);
int mk1 = BitVector32.CreateMask(); //建立第一个 Mask, 或写作 BitVector32.CreateMask(0);
int mk2 = BitVector32.CreateMask(mk1); //在 mk1 后建立 mk2
int mk3 = BitVector32.CreateMask(mk2); //在 mk2 后建立 mk3
int mk4 = BitVector32.CreateMask(mk3); //
int mk5 = BitVector32.CreateMask(mk4); //
//mk1 .. mk5 的值分别会是: 1、2、4、8、16
bv[mk3] = true;
string s1 = bv.ToString(); //BitVector32{00000000000000000000000000000100}
bv[mk1] = true;
bv[mk2] = true;
bv[mk3] = true;
bv[mk4] = true;
bv[mk5] = true;
string s2 = bv.ToString(); //BitVector32{00000000000000000000000000011111}
bv[-1] = true; //全置位
bv[mk1 | mk2 | mk3 | mk4 | mk5] = false; //可以使用 "|" 或 "+" 同时给几个 Mask 赋值
string s3 = bv.ToString(); //BitVector32{11111111111111111111111111100000}
TextBox1.Text = s1 + "\n" + s2 + "\n" + s3;
}
BitVector32.CreateSection():
//假如要使用 0..7 八个数字, 只需要二进制的 3 个位就可以了;
//BitVector32.CreateSection(n) 就是在 BitVector32 中建立能放下 0..n 个小整数的 Section(片段)
protected void Button1_Click(object sender, EventArgs e)
{
BitVector32 bv = new BitVector32(0);
BitVector32.Section sec = BitVector32.CreateSection(7);
string s0, s1, s2, s3, s4, s5, s6, s7;
bv[sec] = 0; s0 = bv.ToString(); //BitVector32{00000000000000000000000000000000}
bv[sec] = 1; s1 = bv.ToString(); //BitVector32{00000000000000000000000000000001}
bv[sec] = 2; s2 = bv.ToString(); //BitVector32{00000000000000000000000000000010}
bv[sec] = 3; s3 = bv.ToString(); //BitVector32{00000000000000000000000000000011}
bv[sec] = 4; s4 = bv.ToString(); //BitVector32{00000000000000000000000000000100}
bv[sec] = 5; s5 = bv.ToString(); //BitVector32{00000000000000000000000000000101}
bv[sec] = 6; s6 = bv.ToString(); //BitVector32{00000000000000000000000000000110}
bv[sec] = 7; s7 = bv.ToString(); //BitVector32{00000000000000000000000000000111}
TextBox1.Text = s0 + "\n" + s1 + "\n" + s2 + "\n" + s3 + "\n" + s4 + "\n" + s5 + "\n" + s6 + "\n" + s7;
}
建立多个 Section:
//使用多个 Section 时, 和 BitVector32.CreateMask() 的理念一样, 是基于一个建另一个, 挨着来.
protected void Button1_Click(object sender, EventArgs e)
{
BitVector32 bv = new BitVector32(0);
BitVector32.Section secX = BitVector32.CreateSection(15); //第一个 Section, 可储存 0..15
BitVector32.Section secY = BitVector32.CreateSection(7, secX); //secX 之后的 Section, 可储存 0..7
BitVector32.Section secZ = BitVector32.CreateSection(15, secY); //secY 之后的 Section, 可储存 0..15
//此时的内存占用图: 000000000000000000000ZZZZYYYXXXX
bv[secX] = 15;
bv[secY] = 0;
bv[secZ] = 15;
string s1 = bv.ToString(); //BitVector32{00000000000000000000011110001111}
bv[secX] = 1;
bv[secY] = 1;
bv[secZ] = 1;
string s2 = bv.ToString(); //BitVector32{00000000000000000000000010010001}
TextBox1.Text = s1 + "\n" + s2;
}
BitVector32.CreateSection() 返回的 BitVector32.Section 也是个结构体:
protected void Button1_Click(object sender, EventArgs e)
{
BitVector32 bv = new BitVector32(0);
BitVector32.Section secX = BitVector32.CreateSection(15);
BitVector32.Section secY = BitVector32.CreateSection(7, secX);
BitVector32.Section secZ = BitVector32.CreateSection(15, secY);
TextBox1.Text = string.Format("{0}, {1}, {2}\n", secX.Mask, secY.Mask, secZ.Mask); //15, 7, 15
TextBox1.Text += string.Format("{0}, {1}, {2}\n", secX.Offset, secY.Offset, secZ.Offset); //0, 4, 7
}