C# - The BitArray Class

The BitArray class provides a number of properties and methods that you use to work with a bitarray.

Properties and Methods of the BitArray Class

Item Description
Properties
Count Contains the number of items currently in the bitarray
IsReadOnly Contains True if the bitarray is read-only and False otherwise
IsSynchronized Contains True if the bitarray is synchronized and False otherwise
Item[] Contains an index of all the items in the bitarray
Length Enables the developer to get and change the length of the bitarray
Methods
And() Performs the logical 'And' operation on the bitarray
Clone() Returns a copy of the bitarray
CopyTo() Copies the bitarray to another type of collection
Get() Returns the value of a specific location in the bitarray
Not() Performs the logical 'Not' operation on the bitarray
Or() Performs the logical 'Or' operation on the bitarray
Set() Sets the value of a specific location in the bitarray
SetAll() Sets all the values in the bitarray
Xor() Performs the logical 'Xor' operation on the bitarray
BitArray.Count

Syntax

Int32 Count

Description

The BitArray.Count property contains the number of elements in the bitarray.

Example

BitArray b = new BitArray(3);
b.Set(0, true);
b.Set(1, true);
b.Set(2, false);


MyLabel.Text = "Number of members: "+b.Count.ToString();
BitArray.IsReadOnly

Syntax

Boolean IsReadOnly

Description

The BitArray.IsReadOnly contains a Boolean value that is used to determine the read-only status of the bitarray.

Example

BitArray b = new BitArray(3);
b.Set(0, true);
b.Set(1, true);
b.Set(2, false);


MyLabel.Text = "Is BitArray read-only?: " + b.IsReadOnly.ToString();
BitArray.IsSynchronized

Syntax

Boolean IsSynchronized

Description

The BitArray.IsSynchronized property contains a Boolean value that is used to determine whether the bitarray is synchronized.

Example

BitArray b = new BitArray(3);
b.Set(0, true);
b.Set(1, true);
b.Set(2, false);


MyLabel.Text = "Is BitArray synchronized?: " + b.IsSynchronized.ToString();
BitArray.Item[]

Syntax

Boolean BitArray.Item[ Int32 index]

Description

The BitArray.Item[] property is used to access individual elements of a bitarray.

Example

BitArray b = new BitArray(3);
b.Set(0, true);
b.Set(1, true);
b.Set(2, false);


MyLabel.Text = "Second element is " + b[1].ToString();
BitArray.Length

Syntax

Int32 Length

Description

The BitArray.Length property can be used to both access and change the length of the bitarray.

Example

BitArray b = new BitArray(3);
b.Set(0, true);
b.Set(1, true);
b.Set(2, false);


ba.Length = 40;

MyLabel.Text = "Length of array: " + b.Length.ToString();
BitArray.And()

Syntax

BitArray And( BitArray value )

Description

The BitArray.And() method is used to perform the 'And' logical operation on a bitarray.

Example

BitArray b = new BitArray(3);
b.Set(0, true);
b.Set(1, true);
b.Set(2, false);


BitArray b2 = new BitArray(3);


BitArray results = new BitArray(3);


results = b.And(b2);
BitArray.Clone()

Syntax

Object Clone()

Description

The BitArray.Clone() method returns an object containing the bitarray on which this operation is being performed.

Example

BitArray b = new BitArray(3);
b.Set(0, true);
b.Set(1, true);
b.Set(2, false);



BitArray results = new BitArray(3);


results = (BitArray)b.Clone();
BitArray.CopyTo()

Syntax

Void CopyTo( Array array, Int32 index )

Description

The BitArray.CopyTo() method copies a bitarray to an array passed in as the first argument, starting at an index provided by the second method argument.

Example

BitArray b = new BitArray(3);
b.Set(0, true);
b.Set(1, true);
b.Set(2, false);


Boolean[] MyArray = new Boolean[3];
b.CopyTo(MyArray, 0);
BitArray.Get()

Syntax

Boolean Get( Int32 index )

Description

The BitArray.Get() method returns the value of a specific element of a bitarray.

Example

BitArray b = new BitArray(3);
b.Set(0, true);
b.Set(1, true);
b.Set(2, false);



Boolean MyBool;
MyBool = b.Get(0);
MyLabel.Text = "MyBool is " + MyBool.ToString();
BitArray.Not()

Syntax

BitArray Not()

Description

The BitArray.Not() method performs the Not logical operation on a bitarray.

Example

BitArray b = new BitArray(3);
b.Set(0, true);
b.Set(1, true);
b.Set(2, false);



BitArray results = new BitArray(3);
results = b.Not();


MyList.DataSource = results;
MyList.DataBind();
BitArray.Or()

Syntax

BitArray Or( BitArray value )

Description

The BitArray.Or() method uses the value argument to perform the Or logical operation on a bitarray.

Example

BitArray b = new BitArray(3);
b.Set(0, true);
b.Set(1, true);
b.Set(2, false);



BitArray b2 = new BitArray(3);
b.Or(b2);
BitArray.Set()

Syntax

Void Set( Int32 index, Boolean value )

Description

The BitArray.Set() method sets an element of the bitarray that is given by the first argument to the value provided in the second argument.

Example

BitArray b = new BitArray(3);
b.Set(0, true);
b.Set(1, true);
b.Set(2, false);
BitArray.SetAll()

Syntax

Void SetAll( Boolean value )

Description

The BitArray.SetAll() method sets all elements of the bitarray to a value provided as the argument to the method.

Example

BitArray b = new BitArray(3);


b.SetAll(true);
BitArray.Xor()

Syntax

BitArray Xor( BitArray value )

Description

The BitArray.Xor() method performs the Xor logical operation on a bitarray, using the value that the bitarray passed in as the argument to the method.

Example

BitArray b = new BitArray(3);
b.Set(0, true);
b.Set(1, true);
b.Set(2, false);


BitArray b2 = new BitArray(3);

b.Xor(b2);