点击这里给我发消息 点击这里给我发消息

学用 ASP.Net 之 System.Collections.BitArray 类

添加时间:2011-6-25
    相关阅读: ASP

常用成员:

/* 属性 */ 
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); 
}

本文作者:未知
咨询热线:020-85648757 85648755 85648616 0755-27912581 客服:020-85648756 0755-27912581 业务传真:020-32579052
广州市网景网络科技有限公司 Copyright◎2003-2008 Veelink.com. All Rights Reserved.
广州商务地址:广东省广州市黄埔大道中203号(海景园区)海景花园C栋501室
= 深圳商务地址:深圳市宝源路华丰宝源大厦606
研发中心:广东广州市天河软件园海景园区 粤ICP备05103322号 工商注册