1

if there are two array, first array size 100 bit and second array size 32 bit,i want to search 32 bit array in first array 100 bit

BitArray bits = new BitArray(100);  
BitArray bitss = new BitArray(32); 


bitss.Set(16,true);
bitss.Set(25,true);
bitss.Set(26,true);
bitss.Set(28,true);
bitss.Set(29,true);
bitss.Set(31,true);

for (int i = 0; i < (bits.Length)-1;i++ )

if ((bits[i] == bitss[0] &&
    bits[i + 1] == bitss[1] &&
    bits[i + 2] == bitss[2] &&
    bits[i + 3] == bitss[3] &&
    ...
    bits[i + 31] == bitss[31]))

    Console.WriteLine("Found");

is there better method for search bitss array in bit array without use ( if and == ) in C#

3 Answers 3

5

How about using LINQ?

for(int i = 0; i < bits.Length - bitss.Length; i++)
{
    if (bits.Cast<bool>().Skip(i).Take(bitss.Length).SequenceEqual(bitss.Cast<bool>()))
        Console.WriteLine("Found!");
}

But just to make it clear: it will be a little slower then using simple for loop!. Bit I would not care about that at all.

Sign up to request clarification or add additional context in comments.

5 Comments

that would make BitArray redundant..Why not create a list of bool then..If he's going to have 1000's of bool casting BitArray to bool would be costly..
@Anirudh LINQ is lazy, so it will get and compare only one item at the time internally. And when you use indexer on BitArray using [] operator there is bool casting performed anyway, so this part of your concerns is not valid either.
@MarcinJuraszek I think Anirudn meant to compare between BitArray and List<bool>, not BitArray and LINQ.
I can't see any List<bool> neither in my solution nor previous comment, so I think it's about LINQ.
@MarcinJuraszek since op has two bitArray it's more convinient to use BitArray.Xor or other bitwise opearation then linq..If you really want to use linq then there's no reason to use BitArray
2

It can definitely be shortened with another for loop:

for (int i = 0; i < (bits.Length)-1;i++ ) {
    bool found = true;
    for (int j = 0; j < 32; j++) {
        if (bits[i + j] != bits[j]) {
            found = false;
            break;
        }
    }
    if (found) Console.WriteLine("Found");
}

Comments

1

Just another option, which i find more readable and debugable :)

for your input, add this method:

public  string BitArrayToString(BitArray input)
{
    StringBuilder sb = new StringBuilder();

    foreach (var bit in input)
    {
        if ((bool) bit) sb.Append("1");
        else sb.Append("0");
    }
    return sb.ToString();
}

and then you can simply do:

BitArray bits = new BitArray(100);
BitArray bitss = new BitArray(32);

bitss.Set(16, true);
bitss.Set(25, true);
bitss.Set(26, true);
bitss.Set(28, true);
bitss.Set(29, true);
bitss.Set(31, true);

var bigString = BitArrayToString(bits);
var smallString = BitArrayToString(bitss);

var cantained = bigString.Contains(smallString);

Done :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.