Mega Code Archive

 
Categories / C# Book / 03 Collections
 

0359 BitArray

A BitArray is a dynamically sized collection of compacted bool values. BitArray has the following methods: public sealed class BitArray : ICollection, IEnumerable, ICloneable { // Constructors public BitArray (BitArray bits); // An existing BitArray to copy public BitArray (int length); // Capacity, in bits public BitArray (bool[] values); public BitArray (byte[] bytes); public BitArray (int[] values); public BitArray (int length, bool defaultValue); // To get/set value public bool this [int index] { get; set; } public bool Get (int index); public void Set (int index, bool value); public void SetAll (bool value); // Bitwise operators public BitArray Not(); public BitArray And (BitArray value); public BitArray Or (BitArray value); public BitArray Xor (BitArray value); // Copying public void CopyTo (Array array, int index); public object Clone(); // Other public IEnumerator GetEnumerator(); public int Count { get; } public int Length { get; set; } public bool IsReadOnly { get; } public bool IsSynchronized { get; } public object SyncRoot { get; } } The following is an example of using the BitArray class: using System; using System.Collections; using System.Collections.Generic; using System.Linq; class Sample { public static void Main() { var bits = new BitArray(2); bits[1] = true; bits.Xor(bits); // Bitwise exclusive-OR bits with itself Console.WriteLine(bits[1]); // False } } The output: False