O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required