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 ...

Get C# Primer: A Practical Approach now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.