C# 在位数组中查找位
有一个位数组,我想在该数组中搜索{00000000000000000110110110} 以便:C# 在位数组中查找位,c#,arrays,bit,C#,Arrays,Bit,有一个位数组,我想在该数组中搜索{00000000000000000110110110} 以便: BitArray bitsarr = new BitArray(150); //array of bits the bits is {00000000000000000000000110110110} its 32 bits ,i want to 搜索如果位在数组bitsarr中找到或没有,如何使用c语言进行搜索,我知道 不需要将{00000000000000000110110110110}
BitArray bitsarr = new BitArray(150); //array of bits
the bits is {00000000000000000000000110110110} its 32 bits ,i want to
搜索如果位在数组bitsarr中找到或没有,如何使用c语言进行搜索,我知道
不需要将{00000000000000000110110110110}转换为十六进制或ineger或任何其他类型 它必须保留位,只需要在bitsarr中搜索位的方法???试试这个
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;
}
静态bool findbits(位数组位,bool[]bitstofind)
{
IEnumerator e=位。GetEnumerator();
List bitsc=新列表();
while(如MoveNext())
位添加((布尔)e.Current);
for(int i=0;i+bitstofind.Length
注1:0为假,1为真
注2:必须使用布尔数组(转换或否)实例化位数组。您也可以使用一些简单的按位操作在位数组中搜索位模式。例如,对两个位使用异或,如果它们匹配,则返回0,否则返回1。您可以使用shift操作(我还没有找到任何方法 在C#中,您可以使用扩展方法(参见[1])向实例化类“添加”公共方法 在这种情况下,您可以执行以下操作:
namespace StackOverFlow
{
static class Program
{
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位长模式的情况
namespace StackOverFlow
{
static class Program
{
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();
}
}
}
命名空间堆栈溢出
{
静态类程序
{
找到公共静态bool(此System.Collections.BitArray文本,System.Collections.BitArray模式)
{
uint B=0;
for(ushort i=0;i B |=num你真的需要澄清这个问题,因为不清楚你所说的“搜索位”{000000000000000000110110110110}这是我想搜索的位链接:[1][2][3][4]Dömölki-(Baeza Yates)-Gonnet算法:[5]位图算法:[6]移位和算法[7]