惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
Stack Overflow Blog
Stack Overflow Blog
G
Google Developers Blog
Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
L
LangChain Blog
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
Y
Y Combinator Blog
月光博客
月光博客
雷峰网
雷峰网
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
F
Fortinet All Blogs
A
About on SuperTechFans
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
小众软件
小众软件
H
Help Net Security
Last Week in AI
Last Week in AI
B
Blog RSS Feed
宝玉的分享
宝玉的分享
N
Netflix TechBlog - Medium
博客园 - 叶小钗
The GitHub Blog
The GitHub Blog

博客园 - 超薄

符号执行虚假控制流去混淆 ios 重启usb ubuntu 搜狗输入法安装 js 反hook js 源码乱码 js和html加上混淆 js 字体加密 jj混淆 webpack 暴力方式扣取 志远js调试相关 并行算法中的异常 事件模式实现通知 扩展属性应用 位运算应用口诀和实例(转自大笨狼) 数据结构实际应用----订单排序(堆排序求前N大) 数据结构Tire 树实际应用----过滤禁词 动态规划 快速排序算法 字典 DictionaryBase 和 SortedList 模式匹配文本处理
位运算
超薄 · 2012-02-11 · via 博客园 - 超薄

一个求素数的例子

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进制表示09的整数

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();

    }

}

移位运算符

二进制数由01组成,数内的每一位置都可以表示成0或者2的次幂

1<<1  结果00000010 

2>>1 就回到1

数值二进制表示 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;

    }

}