/* 属性 */ Count //只读 Length //同 Count, 但可读写 /* 方法 */ And() //与 Get() //取值 Not() //取反 Or() //或 Set() //赋值 SetAll() //全部设定为指定值 Xor() //异或
//(items)[]、Get()、Set() protected void Button1_Click(object sender, EventArgs e) { BitArray bits = new BitArray(new bool[3] { true, false, true }); bool b1 = bits[0]; //True bool b2 = bits[1]; //False //bool b1 = bits.Get(0); //bool b2 = bits.Get(1); bits[0] = false; bits[1] = true; //bits.Set(0, false); //bits.Set(1, true); bool b11 = bits[0]; //False bool b22 = bits[1]; //True TextBox1.Text = string.Concat(b1, "\n", b2, "\n", b11, "\n", b22); } //SetAll() 及遍历 protected void Button2_Click(object sender, EventArgs e) { BitArray bits = new BitArray(5); string str1 = ""; foreach (bool b in bits) { str1 += b.ToString() + " "; //False False False False False } bits.SetAll(true); string str2 = ""; foreach (bool b in bits) { str2 += b.ToString() + " "; //True True True True True } TextBox1.Text = str1 + "\n" + str2; } //Count、Length protected void Button3_Click(object sender, EventArgs e) { BitArray bits = new BitArray(5); int n1 = bits.Count; //5 bits.Length = 3; int n2 = bits.Count; //3 TextBox1.Text = string.Concat(n1, "\n", n2); } //Not() protected void Button4_Click(object sender, EventArgs e) { BitArray bits = new BitArray(new bool[4] { true, false, true, false }); bits.Not(); // False,True, False,True TextBox1.Text = string.Format("{0},{1},{2},{3}", bits[0], bits[1], bits[2], bits[3]); } //And() protected void Button5_Click(object sender, EventArgs e) { BitArray bits1 = new BitArray(new bool[4] { true, false, true, false }); BitArray bits2 = new BitArray(new bool[4] { true, true, false, false }); bits1.And(bits2); // True, False, False, False TextBox1.Text = string.Format("{0},{1},{2},{3}", bits1[0], bits1[1], bits1[2], bits1[3]); } //Or() protected void Button6_Click(object sender, EventArgs e) { BitArray bits1 = new BitArray(new bool[4] { true, false, true, false }); BitArray bits2 = new BitArray(new bool[4] { true, true, false, false }); bits1.Or(bits2); // True, True, True, False TextBox1.Text = string.Format("{0},{1},{2},{3}", bits1[0], bits1[1], bits1[2], bits1[3]); } //Xor() protected void Button7_Click(object sender, EventArgs e) { BitArray bits1 = new BitArray(new bool[4] { true, false, true, false }); BitArray bits2 = new BitArray(new bool[4] { true, true, false, false }); bits1.Xor(bits2); // False,True, True, False TextBox1.Text = string.Format("{0},{1},{2},{3}", bits1[0], bits1[1], bits1[2], bits1[3]); } //其它建立方法 protected void Button8_Click(object sender, EventArgs e) { //对象包含 3 个 true BitArray bits1 = new BitArray(3, true); //以 byte[] 为参数时, 将把其每个 byte 的 8 个位分别映射为一个元素 BitArray bits2 = new BitArray(new byte[3] { byte.MaxValue, byte.MinValue, byte.MaxValue }); //以 int[] 为参数时, 将把其每个 int 的 32 个位分别映射为一个元素 BitArray bits3 = new BitArray(new int[3] { int.MaxValue, int.MinValue, int.MaxValue }); int n1 = bits1.Length; //3 int n2 = bits2.Length; //24 int n3 = bits3.Length; //96 string s1, s2, s3; s1 = s2 = s3 = ""; foreach (bool b in bits1) { s1 = string.Concat(s1, Convert.ToByte(b)); } //111 foreach (bool b in bits2) { s2 = string.Concat(s2, Convert.ToByte(b)); } //11111111 //00000000 //11111111 foreach (bool b in bits3) { s3 = string.Concat(s3, Convert.ToByte(b)); } //11111111111111111111111111111110 //00000000000000000000000000000001 //11111111111111111111111111111110 TextBox1.Text = string.Concat(n1, "\n", n2, "\n", n3, "\n", s1, "\n", s2, "\n", s3); }