Csharp Bitarray
[ C# Collections](#)
BitArray is a collection in C# used to represent a set of bit values.
BitArray belongs to the `System.Collections` namespace and is primarily used for handling binary data or performing bit operations. Compared to using a boolean array (`bool[]`), BitArray is more efficient because it stores each bit in a compact manner.
The BitArray class manages a compact array of bit values, represented using booleans, where `true` indicates the bit is on (1), and `false` indicates the bit is off (0).
You use a BitArray when you need to store bits but don't know the number of bits in advance. You can access items in the BitArray collection using an **integer index**, which starts from zero.
The following table lists some common **properties** of the **BitArray** class:
| Property Name | Type | Description |
| --- | --- | --- |
| `Count` | `int` | Gets the number of bits in the `BitArray` (read-only). |
| `Length` | `int` | Gets or sets the number of bits in the `BitArray` (supports dynamic resizing). |
| `IsReadOnly` | `bool` | Gets a value indicating whether the `BitArray` is read-only (always `false`). |
| `SyncRoot` | `object` | Gets an object that can be used to synchronize access to the `BitArray`. |
| `IsSynchronized` | `bool` | Gets a value indicating whether access to the `BitArray` is synchronized (thread-safe). |
| `Item` | `bool` | Gets or sets the bit value at the specified index (accessed via an indexer). |
Property Access:
```csharp
BitArray bits = new BitArray(8);
Console.WriteLine(bits.Length); // Output: 8
bits.Length = 10; // Modify length to 10
The following table lists some common **methods** of the **BitArray** class:
| Method Name | Return Type | Description |
| --- | --- | --- |
| `And(BitArray)` | `BitArray` | Performs a bitwise AND operation between two `BitArray` objects, returning the result. |
| `Or(BitArray)` | `BitArray` | Performs a bitwise OR operation between two `BitArray` objects, returning the result. |
| `Xor(BitArray)` | `BitArray` | Performs a bitwise XOR operation between two `BitArray` objects, returning the result. |
| `Not()` | `BitArray` | Performs a bitwise NOT operation on the `BitArray`, flipping each bit, and returns the result. |
| `Set(int index, bool value)` | `void` | Sets the bit at the specified index to the specified value (`true` or `false`). |
| `SetAll(bool value)` | `void` | Sets all bits to the specified value (`true` or `false`). |
| `Get(int index)` | `bool` | Gets the bit value at the specified index. |
| `Clone()` | `object` | Creates a shallow copy of the current `BitArray`. |
| `CopyTo(Array array, int index)` | `void` | Copies the elements of the `BitArray` to an existing one-dimensional Array, starting at the specified index. |
| `Equals(object obj)` | `bool` | Determines whether the specified object is equal to the current `BitArray`. |
| `GetEnumerator()` | `IEnumerator` | Returns an enumerator that iterates through the bits in the `BitArray`. |
Method Usage:
```csharp
BitArray bits = new BitArray(8, false); // Create an 8-bit array, all false
bits.Set(0, true); // Set the 0th bit to true
Console.WriteLine(bits.Get(0)); // Output: True
bits.SetAll(true); // Set all bits to true
The following example demonstrates the use of BitArray:
## Example
```csharp
using System;
using System.Collections;
namespace CollectionsApplication
{
class Program
{
static void Main(string[] args)
{
// Initialize two BitArrays to store binary bits
BitArray ba1 = new BitArray(new byte[]{60}); // 60 = 00111100
BitArray ba2 = new BitArray(new byte[]{13}); // 13 = 00001101
// Output the contents of ba1 and ba2
Console.WriteLine("Bit array ba1 (60):");
PrintBitArray(ba1);
Console.WriteLine("Bit array ba2 (13):");
PrintBitArray(ba2);
// Perform AND operation and output the result
BitArray baAnd = ba1.And(ba2); // 60 & 13 = 12
Console.WriteLine("Bit array after AND operation (12):");
PrintBitArray(baAnd);
// Perform OR operation and output the result
BitArray baOr = ba1.Or(ba2); // 60 | 13 = 61
Console.WriteLine("Bit array after OR operation (61):");
PrintBitArray(baOr);
Console.ReadKey();
}
// Output the values of a BitArray
static void PrintBitArray(BitArray bitArray)
{
foreach(bool bit in bitArray)
{
Console.Write($"{(bit ? 1 : 0)} ");
}
Console.WriteLine("n");
}
}
}
When the above code is compiled and executed, it produces the following result:
Bit array ba1 (60):
0 0 1 1 1 1 0 0
Bit array ba2 (13):
1 1 0 1 0 0 0 0
Bit array after AND operation (12):
0 0 0 1 0 0 0 0
Bit array after OR operation (61):
1 1 1 1 1 1 0 0
[ C# Collections](#)
YouTip