BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET Framework
.NET 1.1+

Bit Manipulation with the BitArray

The forty-second part of the C# Fundamentals tutorial examines the use of the BitArray class. This type of collection can be used to hold very large series of bits that can be manipulated either independently or collectively as an entire group.

The BitArray Collection

The BitArray collection can hold large groups of Boolean values or bit fields. It is similar to other collections that have been discussed in the C# Fundamentals tutorial. However, the contents of the collection are not objects but Boolean values. This means that the behaviour of the class differs slightly.

Implemented Collection Interfaces

The BitArray collection implements the properties and methods of the ICollection interface. This behaviour is described in the earlier Collection Interfaces article. The rest of this article describes the additional functionality provided by BitArrays.

Declaring a BitArray

Constructors

The BitArray class is unusual in that it does not provide a parameterless constructor. BitArrays collection must be fully initialised when instantiated using one of six constructors. Each populates the contents of the collection in a different way. The simplest constructor creates a BitArray containing a number of Boolean values that are all false. The number of bits required is passed as an integer argument.

The BitArray class is found in the System.Collections namespace so to execute the examples, add using System.Collections; to the source code.

BitArray flags = new BitArray(16);

This constructor can be extending by passing a second, Boolean value. In this case, the created BitArray is sized according to the integer parameter and each element in the collection is set to the Boolean value.

BitArray flags = new BitArray(16, true);

The remaining four constructors allow a BitArray to be created with its entire contents initialised from existing values. The first of these permits an array of Boolean values to be copied into the collection.

bool[] bits = new bool[] { true, false, false, true };

BitArray flags = new BitArray(bits);

foreach (bool flag in flags)
{
    Console.WriteLine(flag);
}

/* OUTPUT

True
False
False
True

*/

Instead of using an array of Boolean values to initialise a BitArray, an existing BitArray can be used for source data to create a copy.

bool[] bits = new bool[] { true, false, false, true };

BitArray flags = new BitArray(bits);
BitArray copy = new BitArray(flags);

foreach (bool flag in copy)
{
    Console.WriteLine(flag);
}

/* OUTPUT

True
False
False
True

*/

The fifth constructor generates the BitArray contents from an array of bytes. Each byte is split into eight bits in the created collection. The first byte is copied to the first eight bits of the BitArray with the least significant bit as the first item in the collection. The second byte is copied to indexes eight to fifteen and so on.

byte[] bytes = new byte[] { 1, 170 };

BitArray flags = new BitArray(bytes);

foreach (bool flag in flags)
{
    Console.WriteLine(flag);
}

/* OUTPUT

True
False
False
False
False
False
False
False
False
True
False
True
False
True
False
True

*/

The final constructor initialises the contents of the BitArray from an array of integers. Integer values are thirty-two bit values so the first integer is copied into entries zero to thirty-one, the second into bits thirty-two to sixty-three and so on. For each integer, the least significant bit is added first.

int[] values = new int[] { 1, 32767 };

BitArray flags = new BitArray(values);

foreach (bool flag in flags)
{
    Console.WriteLine(flag);
}

/* OUTPUT

True
False
False
False
...

*/
24 June 2007