找到位数组中的位

时间:2013-10-25 17:14:58

标签: c# arrays bit

有位数组,我想在该数组中搜索{00000000000000000000000110110110}

这样:

BitArray bitsarr = new BitArray(150);  //array of bits

the bits is {00000000000000000000000110110110} its 32 bits ,i want to 

搜索数据位是否在数组中找到,如何使用c#语言,我做

无需将{00000000000000000000000110110110}转换为十六进制或ineger或任何其他类型

它必须保留位,只需要在bitsarr中搜索位的方法

3 个答案:

答案 0 :(得分:1)

您也可以使用一些简单的按位运算在位数组中搜索位模式。例如,对两位使用XOR如果匹配则返回0,否则返回1。您可以使用移位操作(<<)来移位位并检查模式中的下一位。

答案 1 :(得分:0)

试试这个

static bool findbits(BitArray bits, bool[] bitstofind)
  {
    IEnumerator e = bits.GetEnumerator();
    List<bool> bitsc = new List<bool>();
    while (e.MoveNext())
        bitsc.Add((bool)e.Current);
    for (int i = 0; i + bitstofind.Length < bitsc.Count; i++)
        if (bitsc.GetRange(i, bitstofind.Length).ToArray().SequenceEqual(bitstofind)) return true;
    return false;
  }

注1:0为假,1为真

注2:BitArray必须使用bool数组实例化(转换或否)

答案 2 :(得分:0)

我还没有找到任何方法。

在C#中,您可以使用扩展方法(参见[1])将“公共方法”“添加”到实例化的类中。

在这种情况下,你可以这样做:

  

命名空间StackOverFlow   {       静态类程序       {

    public static bool Find(this System.Collections.BitArray text, System.Collections.BitArray pattern)
    {
        //... implement your search algorithm here...
    }


    static void Main(string[] args)
    {
        System.Collections.BitArray bitsarr = new System.Collections.BitArray(150);

        bool result = bitsarr.Find(new System.Collections.BitArray(new bool[]{true, true, false, true}));
        Console.WriteLine("Result: {0}", result);
    }


}

}

你可以在网上找到几个匹配的算法,我在这个答案的底部添加了两个链接[2,3]。

对于您的特殊情况,当搜索的字符串为32位时,我推荐使用Dömölki-(Baeza-Yates)-Gonnet算法[4,5,6,7]。

由于您的可能字符集包含2个元素(0和1),因此此修改可能仅适用于您,并且仅在您搜索32位长模式的情况下。

  

命名空间StackOverFlow   {       静态类程序       {

    public static bool IsFound(this System.Collections.BitArray text, System.Collections.BitArray pattern)
    {
        uint B = 0;
        for (ushort i = 0; i < pattern.Length; i++)
        {
            if (pattern[i])
            {
                uint num = 1;
                B |= num << i;
            }
        }
        return IsFound(text, B);
    }

    public static bool IsFound(this System.Collections.BitArray text, uint B)
    {
        uint nB = ~B;
        uint D = 0;
        const uint end = ((uint)1) << 31;
        for (int i = 0; i < text.Length; i++ )
        {
            uint uD = (D << 1) + 1;
            D = uD & (text[i] ? B : nB);
            if ((D & end) > 0)
            {
                return true;
            }
        }
        return false;
    }

    static void Main(string[] args)
    {
        System.Collections.BitArray bitsarr = new System.Collections.BitArray(150);

        //Tests:
        bitsarr[0] = true;
        bitsarr[1] = true;
        bitsarr[2] = false;
        bitsarr[3] = true;

        bitsarr[50] = true;
        bitsarr[51] = true;
        bitsarr[52] = true;
        bitsarr[53] = false;
        bitsarr[54] = false;
        bool result = bitsarr.IsFound(new System.Collections.BitArray(new bool[]{true, true, false, true}));
        Console.WriteLine("Result: {0}, expected True", result);
        result = bitsarr.IsFound(new System.Collections.BitArray(new bool[] { true, true, true, true }));
        Console.WriteLine("Result: {0}, expected False", result);
        result = bitsarr.IsFound(new System.Collections.BitArray(new bool[] { true, true, true, false }));
        Console.WriteLine("Result: {0}, expected True", result);
        result = bitsarr.IsFound(new System.Collections.BitArray(new bool[] { false, true, true, true }));
        Console.WriteLine("Result: {0}, expected True", result);
        result = bitsarr.IsFound(new System.Collections.BitArray(new bool[] { false, true, true, true }));
        Console.WriteLine("Result: {0}, expected True", result);
        Console.ReadKey();
    }


}

}

注意:需要进一步测试。

(我不能在这里添加链接,因为我需要10个声望)

相关问题