學用 ASP.Net 之 System.Collections.BitArray 類

來源:互聯網
上載者:User
常用成員:
/* 屬性 */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、Lengthprotected 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);}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.