Storing Bits with BitArray


The BitArray class supports a collection of bits. Because it stores bits rather than objects, BitArray has capabilities different from those of the other collections. However, it still supports the basic collection underpinning by implementing ICollection and IEnumerable. It also implements ICloneable.

BitArray defines several constructors. You can construct a BitArray from an array of Boolean values using this constructor:

 public BitArray(bool[ ] bits)

In this case, each element of bits becomes a bit in the collection. Thus, each bit in the collection corresponds to an element of bits. Furthermore, the ordering of the elements of bits and the bits in the collection are the same.

You can create a BitArray from an array of bytes using this constructor:

 public BitArray(byte[ ] bits)

Here, the bit pattern in bits becomes the bits in the collection, with bits[0] specifying the first 8 bits, bits[1] specifying the second 8 bits, and so on. In similar fashion, you can construct a BitArray from an array of ints using this constructor:

 public BitArray(int[ ] bits)

In this case, bits[0] specifies the first 32 bits, bits[1] specifies the second 32 bits, and so on.

You can create a BitArray of a specific size using this constructor:

 public BitArray(int size)

Here, size specifies the number of bits. The bits in the collection are initialized to false.

To specify a size and initial value of the bits, use the following constructor:

 public BitArray(int size, bool v)

In this case, all bits in the collection will be set to the value passed in v.

Finally, you can create a new BitArray from an existing one by using this constructor:

 public BitArray(BitArray bits)

The new object will contain the same collection of bits as bits, but the two collections will be otherwise separate.

BitArrays can be indexed. Each index specifies an individual bit, with an index of zero indicating the low-order bit.

In addition to the methods specified by the interfaces that it implements, BitArray defines the methods shown in Table 23-9. Notice that BitArray does not supply a Synchronized( ) method. Thus, a synchronized wrapper is not available, and the IsSynchronized property is always false. However, you can control access to a BitArray by synchronizing on the object provided by SyncRoot.

Table 23-9: The Methods Defined by BitArray

Method

Description

public BitArray And(BitArray ba)

ANDs the bits of the invoking object with those specified by ba and returns a BitArray that contains the result.

public bool Get(int idx)

Returns the value of the bit at the index specified by idx.

public BitArray Not( )

Performs a bitwise, logical NOT on the invoking collection and returns a BitArray that contains the result.

public BitArray Or(BitArray ba)

ORs the bits of the invoking object with those specified by ba and returns a BitArray that contains the result.

public void Set(int idx, bool v)

Sets the bit at the index specified by idx to v.

public void SetAll(bool v)

Sets all bits to v.

public BitArray Xor(BitArray ba)

XORs the bits of the invoking object with those specified by ba and returns a BitArray that contains the result.

To the properties specified by the interfaces that it implements, BitArray adds Length, which is shown here:

 public int Length { get; set; }

Length sets or obtains the number of bits in the collection. Thus, Length gives the same value as does the standard Count property, which is defined for all collections. However, Count is read-only, but Length is not. Thus, Length can be used to change the size of a BitArray. If you shorten a BitArray, bits are truncated from the high-order end. If you lengthen a BitArray, false bits are added to the high-order end.

BitArray defines the following indexer:

 public bool this[int idx] { get; set; }

You can use this indexer to get or set the value of an element.

Here is an example that demonstrates BitArray:

 // Demonstrate BitArray. using System; using System.Collections; class BADemo {   public static void showbits(string rem,                          BitArray bits) {     Console.WriteLine(rem);     for(int i=0; i < bits.Count; i++)       Console.Write("{0, -6} ", bits[i]);     Console.WriteLine("\n");   }   public static void Main() {     BitArray ba = new BitArray(8);     byte[] b = { 67 };     BitArray ba2 = new BitArray(b);     showbits("Original contents of ba:", ba);     ba = ba.Not();     showbits("Contents of ba after Not:", ba);     showbits("Contents of ba2:", ba2);     BitArray ba3 = ba.Xor(ba2);     showbits("Result of ba XOR ba2:", ba3);   } }

The output is shown here:

 Original contents of ba: False  False  False  False  False  False  False  False Contents of ba after Not: True   True   True   True   True   True   True   True Contents of ba2: True   True   False  False  False  False  True   False Result of ba XOR ba2: False  False  True   True   True   True   False  True




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net