一个求素数的例子
public void GenPrimes()
{
for (int outer = 2; outer <= arr.GetUpperBound(0); outer++)
for (int inner = outer + 1; inner <= arr.GetUpperBound(0); inner++)
if (arr[inner] == 1)
if ((inner % outer) == 0)
arr[inner] = 0;
}
public void ShowPrimes()
{
for (int i = 2; i <= arr.GetUpperBound(0); i++)
if (arr[i] == 1)
Console.Write(i + " ");
}
static void Main()
{
int size = 100;
CArray primes = new CArray(size - 1);
for (int i = 0; i <= size - 1; i++)
primes.Insert(1);
primes.GenPrimes();
primes.ShowPrimes();
}
位和位操作
2进制表示0到9的整数
0000000----0d(d表示10进制数)
0000001-----1d
0000010-----2d
00000011----3d
00000100-----4d
00000101-----5d
.......................
处理二进制数:按位运算符和移位运算符
对于二进制数必须采用按位运算符(And .Or ,Not)或者移位运算符(>> ,<< ,>>>)
And
1 |
1 |
1 |
1 |
0 |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
Or
1 |
1 |
1 |
1 |
0 |
1 |
0 |
1 |
1 |
0 |
0 |
0 |
Xor
1 |
1 |
0 |
1 |
0 |
1 |
0 |
1 |
1 |
0 |
0 |
0 |
按位运算应用
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnClear;
private System.Windows.Forms.Button btnOr;
private System.Windows.Forms.Button btnXor;
private System.Forms.Label lblInt1Bits;
private System.Forms.Label lblInt2Bits;
private System.Forms.TextBox txtInt1;
private System.Forms.TextBox txtInt2;
// other Windows app code here
private void btnAdd_Click(object sender, System.EventArgs e)
{
int val1, val2;
val1 = Int32.Parse(txtInt1.Text);
val2 = Int32.Parse(txtInt2.Text);
lblInt1Bits.Text = ConvertBits(val1).ToString();
lblInt2Bits.Text = ConvertBits(val2).ToString();
}
private StringBuilder ConvertBits(int val)
{
int dispMask = 1 << 31;
StringBuilder bitBuffer = new StringBuilder(35);
for (int i = 1; i <= 32; i++)
{
if ((val && bitMask) == 0)
bitBuffer.Append("0");
else
bitBuffer.Append("1");
val <<= 1;
if ((i % 8) == 0)
bitBuffer.Append(" ");
}
return bitBuffer;
}
private void btnClear_Click(object sender, System.Eventargs e)
{
txtInt1.Text = "";
txtInt2.Text = "";
lblInt1Bits.Text = "";
lblInt2Bits.Text = "";
lblBitResult.Text = "";
txtInt1.Focus();
}
private void btnOr_Click(object sender, System.EventsArgs e)
{
int val1, val2;
val1 = Int32.Parse(txtInt1.Text);
val2 = Int32.Parse(txtInt2.Text);
lblInt1Bits.Text = ConvertBits(val1).ToString();
lblInt2Bits.Text = ConvertBits(val2).ToString();
lblBitResult.Text = ConvertBits(val1 ||
val2).ToString();
}
private void btnXOr_Click(object sender, System.EventsArgs e)
{
int val1, val2;
val1 = Int32.Parse(txtInt1.Text);
val2 = Int32.Parse(txtInt2.Text);
lblInt1Bits.Text = ConvertBits(val1).ToString();
lblInt2Bits.Text = ConvertBits(val2).ToString();
lblBitResult.Text = ConvertBits(val1 ^ val2).ToString();
}
}
移位运算符
二进制数由0和1组成,数内的每一位置都可以表示成0或者2的次幂
1<<1 结果00000010
2>>1 就回到1
数值3 二进制表示 00000011
3<<1 结果00000110
3>>1 结果0000001
整数转换成二进制
sing System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
public class Form1 : System.Windows.Forms.Form
{
// Windows generated code omitted here
private void btnOr_Click(object sender,
System.EventsArgs e)
{
int val1, val2;
val1 = Int32.Parse(txtInt1.Text);
val2 = Int32.Parse(txtInt2.Text);
lblInt1Bits.Text = ConvertBits(val1).ToString();
lblInt2Bits.Text = ConvertBits(val2).ToString();
lblBitResult.Text = ConvertBits(val1 || val2).
ToString();
}
private StringBuilder ConvertBits(int val)
{
int dispMask = 1 << 31;
StringBuilder bitBuffer = new StringBuilder(35);
for (int i = 1; i <= 32; i++)
{
if ((val && bitMask) == 0)
bitBuffer.Append("0");
else
bitBuffer.Append("1");
val <<= 1;
if ((i % 8) == 0)
bitBuffer.Append(" ");
}
return bitBuffer;
}
}
移位运算实例
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
public class Form1 : System.Windows.Forms.Form
{
// Windows generated code omitted
private StringBuilder ConvertBits(int val)
{
int dispMask = 1 << 31;
StringBuilder bitBuffer = new StringBuilder(35);
for (int i = 1; i <= 32; i++)
{
if ((val && bitMask) == 0)
bitBuffer.Append("0");
else
bitBuffer.Append("1");
val <<= 1;
if ((i % 8) == 0)
bitBuffer.Append(" ");
}
return bitBuffer;
}
private void btnOr_Click(object sender, System.EventsArgs e)
{
txtInt1.Text = "";
txtBitShift.Text = "";
lblInt1Bits.Text = "";
lblOrigBits.Text = "";
txtInt1.Focus();
}
private void btnLeft_Click(object sender, System.EventsArgs e)
{
int value = Int32.Parse(txtInt1.Text);
lblOrigBits.Text = ConvertBits(value).ToString();
value <<= Int32.Parse(txtBitShift.Text);
lblInt1Bits.Text = ConvertBits(value).ToString();
}
private void btnRight_Click(object sender, System.EventsArgs e)
{
int value = Int32.Parse(txtInt1.Text);
lblOrigBits.Text = ConvertBits(value).ToString();
value >>= Int32.Parse(txtBitShift.Text);
lblInt1Bits.Text = ConvertBits(value).ToString();
}
}
BitArray
BitArray 类似Arraylist 动态变换大小
初始化
BitArray BitSet = new BitArray(32);
BitArray BitSet = new BitArray(32,true);
byte[] ByteSet = new byte[] { 1, 2, 3, 4, 5 };
BitArray BitSet = new BitArray(ByteSet);
遍历
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) + " ");
每个字节的每一位都会输出
按照行输出实例
using System;
using System.Collections;
class chapter6
{
static void Main()
{
int bits;
string[] binNumber = new string[8];
int binary;
byte[] ByteSet = new byte[] { 1, 2, 3, 4, 5 };
BitArray BitSet = new BitArray(ByteSet);
bits = 0;
binary = 7;
for (int i = 0; i <= BitSet.Count - 1; i++)
{
if (BitSet.Get(i) == true)
binNumber[binary] = "1";
else
binNumber[binary] = "0";
bits++;
binary--;
if ((bits % 8) == 0)
{
binary = 7;
bits = 0;
for (int j = 0; j <= 7; j++)
Console.Write(binNumber[j]);
Console.WriteLine();
}
}
}
}
更多方法和属性
BitArray.Set(bit, value)
bitSet1.Or(bitSet2)
bitSet.Clone()
bitSet.CopyTo(arrBits)
艾拉托斯特泥筛法中使用 Bitarrya
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
public class Form1 : System.Windows.Forms.Form
{
// Windows generated code omitted
private void btnPrime_Click(object sender,
System.EventsArgs e)
{
BitArray[] bitSet = new BitArray[1024];
int value = Int32.Parse(txtValue.Text);
BuildSieve(bitSet);
if (bitSet.Get(value))
lblPrime.Text = (value + " is a prime number.");
else
lblPrime.Text = (value + " is not a prime number.");
}
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 - 1; j++)
bits.Set(j, 0);
int counter = 0;
for (int i = 1; i <= bits.Count - 1; i++)
if (bits.Get(i))
{
primes += i.ToString();
counter++;
if ((counter % 7) == 0)
primes += "\n";
else
primes += "\n";
}
txtPrimes.Text = primes;
}
}