19.2. BitArray

A BitArray is similar to an ArrayList, except that it is used to store only bits represented as boolean values. A 1 bit is true, while a 0 bit is false. BitArray objects are extremely useful for mathematical operations such as those involving long encryption keys. The class has the following public methods which are useful for operations on the bits it encapsulates: Xor, Not, Or, And.

The program below demonstrates how a BitArray can be used. The output is interspersed with the code to show the outputs of the different sections.

 1: using System;
 2: using System.Collections;
 3:
 4: public class TestClass  {
 5:    public static void Main()  {
 6:
 7:       // By default, all bits are false if not specified
 8:       BitArray ba1 = new BitArray(4);
 9:       Console.WriteLine("ba1");
10:       Console.WriteLine("Length:" + ba1.Length);
11:       Console.WriteLine("Values:");
12:       PrintCollection(ba1);
13:

Output:

ba1
Length:4
Values:
False,False,False,False,

Line 8 creates a new BitArray object containing four bits.

14:       // Use of Set method
15:       Console.WriteLine("Setting elements 0 and 2");
16:       ba1.Set(0,true);
17:       ba1.Set(2,true);
18:       PrintCollection(ba1);
19:

Output:

Setting elements 0 and 2
True,False,True,False,

20:       // Accessing a BitArray like an index
21:       Console.WriteLine("ba1[0] : " + ba1[0]);
22:       Console.WriteLine("ba1[1] : " + ba1[1]);
23:       Console.WriteLine("ba1[2] : " + ba1[2]);
24:       Console.WriteLine("ba1[3] : " + ba1[3]);
25:

Output:

ba1[0] : True
ba1[1] : False
ba1[2] : True
ba1[3] : False

A BitArray object can be used like an index [1] as demonstrated in these lines.

[1] Apparently a public indexer is implemented within this class. For more information about C# indexers, see Chapter 21.

26:       // Create a BitArray containing true bits
27:       BitArray ba2 = new BitArray(4, true);
28:       Console.WriteLine("ba2");
29:       Console.WriteLine("Length:" + ba2.Length );
30:       Console.WriteLine("Values:");
31:       PrintCollection(ba2);
32:

Output:

ba2
Length:4
Values:
True,True,True,True,

In this case, a new BitArray object is created with an overloaded constructor which takes in a boolean value too. A BitArray object encapsulating four true bits is created.

33:       // Passing in an array of byte values
34:       byte[] myBytes = new byte[4] {1, 2, 3, 4};
35:       BitArray ba3 = new BitArray(myBytes);
36:       Console.WriteLine("ba3");
37:       Console.WriteLine("Length:" + ba3.Length);
38:       Console.WriteLine("Values:");
39:       PrintCollection(ba3);
40:

Output:

ba3
Length:32
Values:
True,False,False,False,False,False,False,False,
False,True,False,False,False,False,False,False,
True,True,False,False,False,False,False,False,
False,False,True,False,False,False,False,False,

A byte takes up eight bits in C#. On line 35, a new BitArray object is created with a byte array containing four byte elements. In all, the BitArray object will have a size of 32 bits. Within the BitArray, the bits are ordered with the least significant bit first. So, decimal 1 is translated into 10000000 2, 2 is translated into 01000000 2 and are saved as bits in the BitArray. Note that the BitArray object does not keep track of the byte boundaries – as far as it is concerned, it simply stores a consecutive meaningless series of 32 bits.

41:       // Passing in an array of int values
42:       int[]  myInts  = new int[4] {1, 2, 3, 4};
43:       BitArray ba4 = new BitArray(myInts);
44:       Console.WriteLine("ba4");
45:       Console.WriteLine("Length:" + ba4.Length);
46:       Console.WriteLine("Values:");
47:       PrintCollection(ba4);
48:

Output:

ba4
Length:128
Values:
True,False,False,False,False,False,False,False,
False,False,False,False,False,False,False,False,
False,False,False,False,False,False,False,False,
False,False,False,False,False,False,False,False,
False,True,False,False,False,False,False,False,
False,False,False,False,False,False,False,False,
False,False,False,False,False,False,False,False,
False,False,False,False,False,False,False,False,
True,True,False,False,False,False,False,False,
False,False,False,False,False,False,False,False,
False,False,False,False,False,False,False,False,
False,False,False,False,False,False,False,False,
False,False,True,False,False,False,False,False,
False,False,False,False,False,False,False,False,
False,False,False,False,False,False,False,False,
False,False,False,False,False,False,False,False,

In line 43, a new BitArray instance is created with an int array containing four ints. Each int is 16 bits long in C#. Hence decimal 1 is translated into 1000000000000000 2 (least significant bit first).

49:       // Passing in an array of boolean values
50:       bool[] myBools =
51:         new bool[4] {true, false, true, true};
52:       BitArray ba5 = new BitArray(myBools);
53:       Console.WriteLine("ba5");
54:       Console.WriteLine("Length:" + ba5.Length );
55:       Console.WriteLine("Values:");
56:       PrintCollection(ba5);
57:    }
58:

Output:

ba5
Length:4
Values:
True,False,True,True,

The BitArray constructor is also overloaded to take in an array of bools.

59:   /* Print out the contents of the BitArray object
60:    * Only 8 elements are printed on each line for
61:    * easy reading.
62:    */
63:   public static void PrintCollection(IEnumerable bArray){
64:     IEnumerator enumerator = bArray.GetEnumerator();
65:     int i = 0;
66:     while (enumerator.MoveNext()){
67:       if (i>=8){
68:         i = 0;
69:         Console.WriteLine(); // new line
70:       }
71:       i++;
72:       Console.Write(enumerator.Current + ",");
73:     }
74:     Console.WriteLine();
75:   }
76: }

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.174.0