Is there an easy way to combine two BitArray (C # .NET)?

I have

var previous = new BitArray(new bool[]{true}); var current = new BitArray(new bool[]{false}); 

I want to combine them. I already tried:

 var next = new BitArray(previous.Count + current.Count); var index = 0; for(;index < previous.Count; index++) next[index] = previous[index]; var j = 0; for(;index < next.Count; index++, j++) next[index] = current[j]; previous = current; 

But this does not seem to be the best way to do this.

+6
c # bitarray
source share
5 answers

Unfortunately, it looks like your method can be as good as it is - if BitArray implemented IEnumerable <T> (instead of IEnumerable), we could use LINQ extension methods to make it a bit prettier.

If I were you, I would have wrapped this in an extension method in BitArray:

 public static BitArray Prepend(this BitArray current, BitArray before) { var bools = new bool[current.Count + before.Count]; before.CopyTo(bools, 0); current.CopyTo(bools, before.Count); return new BitArray(bools); } public static BitArray Append(this BitArray current, BitArray after) { var bools = new bool[current.Count + after.Count]; current.CopyTo(bools, 0); after.CopyTo(bools, current.Count); return new BitArray(bools); } 
+7
source share

You can do this with LINQ, after Cast<bool>() bitarray 'becomes' IEnumerable<bool> :

 var previous = new BitArray(new bool[] { true }); var current = new BitArray(new bool[] { false }); BitArray newBitArray = new BitArray(previous.Cast<bool>().Concat(current.Cast<bool>()).ToArray()); 

I do not think this LINQ method will be fast.

+5
source share

Structure does not provide a good way to do this. You can create a bools array that is large enough to store both BitArrays. Then use BitArray.CopyTo to copy each BitArray to the bools array (you can specify where to start inserting elements).

After that, create another BitArray with a constructor that accepts a bools array.

A lot of the work that I know, but there seems to be no other way. This is less code than your current method.

+2
source share

Here is my LINQ implementation that does not include the overhead associated with allocating the bools array:

 var result = new BitArray(first.Count + second.Count); var i = 0; foreach (var value in first.Cast<bool>().Concat(second.Cast<bool>())) { result[i++] = value; } 
0
source share

This is more efficient if you use int32 instead of bools, because bitarray uses int32 internally.

 public static BitArray Append(this BitArray current, BitArray after) { var ints = new int[(current.Count + after.Count) / 32]; current.CopyTo(ints, 0); after.CopyTo(ints, current.Count / 32); return new BitArray(ints); } 

In Vb.net, if someone needs this:

 <Runtime.CompilerServices.Extension()> _ Public Function Append(ByVal current As BitArray, ByVal after As BitArray) As BitArray Dim ints = New Int32((current.Count + after.Count) \ 32 - 1) {} current.CopyTo(ints, 0) after.CopyTo(ints, current.Count \ 32) Return New BitArray(ints) End Function 
-one
source share

All Articles