How to check bitArray containing any true or any false value?

In C # and Vb.net, is there any way without iterating over a bitarray loop to check if any true or false value continues (Dotnet 2.0)?

+5
source share
4 answers

I doubt that you could do this without a loop under the hood (since it BitArraycan be arbitrarily long, unlike BitVector32), but if you just don't want to write it yourself:

var hasAnyTrue = input.Cast<bool>().Contains(true);
var hasAnyFalse = input.Cast<bool>().Contains(false);
+6
source

If you use the BitArray class from System.Collections, you can use the following code to determine if something is true.

C # version

var anyTrue = myArray.Cast<bool>().Any(x => x);

Version VB.Net

Dim anyTrue = myArray.Cast(Of Boolean)().Any(Function(x) x)
+2

, BitArray, int, long .., , 0 ( true) ( false), .

- :

bool IsTrue (int bitArray)
{
  return bitArray != 0;
}


bool isFalse (int bitArray)
{
   return bitArray != int.MinValue;
}
+1

BitArray boolean . , , BitArray , , CopyTo, int[] size (Count >> 5), ints, .

+1

All Articles