O'Reilly logo

C# Primer: A Practical Approach by Stanley B. Lippman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

4.9. BitVectorx: Extension through Composition

The BitArray class is provided within the System.Collections name-space. It manages an array of Boolean objects as if it were a bit vector. A true value indicates that the bit is turned on; false, that it is off. Count returns the number of elements in the array. It is a property of the ICollection interface, from which BitArray is derived.

We can turn a bit on or off either by subscripting into the array:

bat1[ 2 ] = bat2[ 4 ] = true;

or by explicitly setting the element:

lbat.Set(ix, bat[ ix ] );

A function called SetAll() sets all the bits in the array to either true or false. The value of an element can be read either through subscripting or by the Get() method:

 // both return the value ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required