BiteArray
using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace BitArray类 { class Program { static void Main(string[] args) { } /* * 埃拉托斯尼筛选法 * 求素数 */ public void GenPrimes(int[] arr) { for(int outer=2;outer<arr.GetUpperBound(0);outer++) for(int inner=outer+1;inner<=arr.GetUpperBound(0);inner++) { if (arr[inner] == 1) if (arr[inner] == 1) arr[inner] = 0; } } /// <summary> /// 显示素数 /// </summary> /// <param name="arr"></param> public void ShowPrimes(int[] arr) { for (int i = 2; i <= arr.GetUpperBound(0); i++) { if (arr[i] == 1) Console.WriteLine(i+"素数!"); } } /* * BitArray类 * 初始化 */ BitArray BitSet = new BitArray(32); /*这个bitarray的32位都被设为true*/ BitArray BitSet = new BitArray(32, true); /*初始化赋值操作*/ byte[] ByteSet = new byte[] { 4,5,6,7,5,3,2,8}; BitArray BitSet = new BitArray(ByteSet); /* * 如果bitarray中有byte值,那么当循环遍历数组的时候每个byte值的每一位都 * 将显示出来。下面这个简单的程序段就循环遍历了byte值的bitArray */ byte[] ByteSet = new byte[] { 1, 2, 3, 4, 5 }; BitArray BitSet = new BitArray(ByteSet); for(int bits=0;bits<=BitSet.Count-1;bits++) { Console.Write(BitSet.Get(bits)+""); } /* * bitArray类用来存储位的集合,尽管位通常会用0,1来表示,但是bitarray类会把这些数值替换为true(1) * 或者False(0)值的形式存储起来,bitArray在需要存储一组boolean行数值的时候是很有用的,在需要处理位时 * 它甚至更加有用,这是因为人们可以很容易的在位值和boolean值之间进行前后移动 */ } }
BitArray类
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Collections; namespace BitArray类 { public partial class 按位运算符运算 : Form { public 按位运算符运算() { InitializeComponent(); } private void btnDisplay_Click(object sender, EventArgs e) { int integer1 = int.Parse(textBox1.Text); int integer2 = int.Parse(textBox2.Text); label3.Text = ConvertBits(integer1).ToString(); label4.Text = ConvertBits(integer2).ToString(); } /// <summary> /// 10进制向2进制8进制的转换 /// </summary> /// <param name="jishu">进制数</param> /// <param name="convert">要转换的数</param> public void ConvertBits(int jishu, int convert) { //先进先出 Stack Digits = new Stack(); do { Digits.Push(convert % jishu); convert/jishu; } while (convert != 0); while (Digits.Count > 0) { Console.WriteLine(Digits.Pop()); } } /// <summary> /// 返回convert的2进制字符串 /// </summary> /// <param name="convert">要转换的数</param> /// <returns>convert的2进制字符串</returns> public StringBuilder ConvertBits(int convert) { int dispMask = 1 << 31; StringBuilder bitBuffer = new StringBuilder(35); for (int i = 1; i <= 32; i++) { if ((convert && dispMask) == 0) { bitBuffer.Append("0"); } else bitBuffer.Append("1"); convert <<= 1; if (i % 8 == 0) bitBuffer.Append(" "); } return bitBuffer; } private void BuildSieve(BitArray bits) { string primes; for (int i = 0; i <= bits.Count - 1; i++) { bits.Set(i,1); } int lastBit = Int32.Parse(Math.Sqrt(bits.Count)); for (int i = 2; i <= lastBit - 1; i++) { if (bits.Get(i)) for (int j = 2 * i; j < bits.Count; j++) bits.Set(j, 0); } int counter = 0; for (int i = 1; i < bits.Count - 1; i++) { if (bits.Get(i)) { primes += i.ToString(); if (counter % 7 == 0) primes += "\n"; else primes += "\n"; } } } private void 按位运算符运算_Load(object sender, EventArgs e) { } private void btnclear_Click(object sender, EventArgs e) { foreach (Control item in this.Controls) { if (item as Button) { item.Enabled = false; } } } } }