Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How check bitArray contains any true or any false value?

Tags:

c#

vb.net

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

like image 435
Nakul Chaudhary Avatar asked Jun 09 '09 11:06

Nakul Chaudhary


2 Answers

I doubt there's any way you could do it without a loop under the hood (as a BitArray can 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);
like image 147
mmx Avatar answered Oct 13 '22 01:10

mmx


If you are using the BitArray class from System.Collections you can use the following code to determine if anything is true.

C# version

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

VB.Net Version

Dim anyTrue = myArray.Cast(Of Boolean)().Any(Function(x) x)
like image 36
JaredPar Avatar answered Oct 13 '22 00:10

JaredPar