title: Parallel-Sequential "for" loop to set bits from bitarray to another bitarray is there any other faster way than this?
body: <p>I have a bitarray1 which is 300000 bits in length and filled with boolean values (true &amp; false). And I have a another bitarray2 of the same size(empty). I need to set bits for bitarray2 if certain conditions are matched in bitarray1. But need to do it sequentially. So, what will be the best way to perform it. I'm focused on speed. So, is it possible to perform this via PLINQ or any other parallel threading? I know parallel and sequence is two opposite things, but there should be an alternative way to do this :) as current method shown below is so slow. </p>
  
  <p>Please see this simple illustration to understand what I really needed, I'm really bad at drawing things, so please bear with me :) <a href="http://i57.tinypic.com/2ry6fjt.jpg" rel="nofollow">link</a></p>
  
  <pre><code>Dim BitPosition1 As Int64 = 0
      Dim counter As Int64 = 0
      For CopyBits As Integer = 0 To Bitarray1.Length
  
          If CopyBits = bitarray1.length - 1 Then
              Exit For
          End If
  
          'if first four bits of "bitarray1" are "0100", then write "01" to "bitarray2" 
          If Bitarray1(BitPosition1) = False AndAlso Bitarray1(BitPosition1 + 1) = True AndAlso Bitarray1(BitPosition1 + 2) = False AndAlso Bitarray1(BitPosition1+3) = False Then
  
              Bitarray2.Set(counter, False)
              Bitarray2.Set(counter + 1, True)
              counter += 2
          End If
  
          'if first four bits of "bitarray1" are "1110", then write "10" to "bitarray2" 
          If Bitarray1(BitPosition1) = True AndAlso Bitarray1(BitPosition1 + 1) = True AndAlso Bitarray1(BitPosition1 + 2) = True AndAlso Bitarray1(BitPosition1+3) = False Then
  
              Bitarray2.Set(counter, True)
              Bitarray2.Set(counter + 1, False)
              counter += 2
          End If
  
          BitPosition1 += 4
  </code></pre>