BitArray类用于管理位值的压缩数组,该值表示为布尔值,其中 true 表示位值为1,false表示位值为0。
BitArray类常用的属性和方法:
属性 | 说明 |
Count | 获取BitArray中包含的元素数。 |
方法 | 说明 |
Get | 获取 BitArray 中特定位置处的位的值。 |
Set | 将 BitArray 中特定位置处的位设置为指定值。 |
SetAll | 将 BitArray 中的所有位设置为指定值。 |
BitArray实例:
public static void Main()
{
// Creates and initializes a BitArray.
BitArray myBA = new BitArray( 5 );
// Displays the properties and values of the BitArray.
Console.WriteLine( "myBA values:" );
PrintIndexAndValues( myBA );
// Sets all the elements to true.
myBA.SetAll( true );
// Displays the properties and values of the BitArray.
Console.WriteLine( "After setting all elements to true," );
PrintIndexAndValues( myBA );
// Sets the last index to false.
myBA.Set( myBA.Count - 1, false );
// Displays the properties and values of the BitArray.
Console.WriteLine( "After setting the last element to false," );
PrintIndexAndValues( myBA );
}
public static void PrintIndexAndValues( IEnumerable myCol )
{
int i = 0;
foreach ( Object obj in myCol )
{
Console.WriteLine( " [{0}]: {1}", i++, obj );
}
Console.WriteLine();
}