Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Gets or sets the value of the bit at a specific position in the BitArray in C#
The BitArray class in C# provides an indexer that allows you to get or set the value of a bit at a specific position. You can access individual bits using either the indexer syntax arr[index] or the Get() and Set() methods.
Syntax
Following is the syntax for accessing bits in a BitArray −
// Using indexer to get/set bits bitArray[index] = true; // Set bit at index bool value = bitArray[index]; // Get bit at index // Using Get() and Set() methods bool value = bitArray.Get(index); // Get bit at index bitArray.Set(index, true); // Set bit at index
Parameters
-
index ? The zero-based index of the bit to access.
-
value ? The boolean value to set (true or false).
Using Indexer to Get and Set Bits
The indexer provides the most direct way to access individual bits in a BitArray −
using System;
using System.Collections;
public class Demo {
public static void Main() {
BitArray arr1 = new BitArray(4);
// Set bits using indexer
arr1[0] = false;
arr1[1] = true;
arr1[2] = false;
arr1[3] = true;
Console.WriteLine("BitArray length = " + arr1.Length);
Console.WriteLine("Bit at index 0: " + arr1[0]);
Console.WriteLine("Bit at index 1: " + arr1[1]);
Console.WriteLine("Bit at index 2: " + arr1[2]);
Console.WriteLine("Bit at index 3: " + arr1[3]);
// Update a bit value
Console.WriteLine("\nUpdating bit at index 0 to true...");
arr1[0] = true;
Console.WriteLine("Bit at index 0 [UPDATED]: " + arr1[0]);
}
}
The output of the above code is −
BitArray length = 4 Bit at index 0: False Bit at index 1: True Bit at index 2: False Bit at index 3: True Updating bit at index 0 to true... Bit at index 0 [UPDATED]: True
Using Get() and Set() Methods
You can also use the Get() and Set() methods to access individual bits −
using System;
using System.Collections;
public class Demo {
public static void Main() {
BitArray arr = new BitArray(3);
// Set bits using Set() method
arr.Set(0, false);
arr.Set(1, true);
arr.Set(2, false);
Console.WriteLine("BitArray elements using Get() method:");
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine("Bit at index " + i + ": " + arr.Get(i));
}
// Update using Set() method
Console.WriteLine("\nUpdating bit at index 2 to true...");
arr.Set(2, true);
Console.WriteLine("Bit at index 2 [UPDATED]: " + arr.Get(2));
// Display all elements
Console.WriteLine("\nAll elements:");
foreach (bool bit in arr) {
Console.WriteLine(bit);
}
}
}
The output of the above code is −
BitArray elements using Get() method: Bit at index 0: False Bit at index 1: True Bit at index 2: False Updating bit at index 2 to true... Bit at index 2 [UPDATED]: True All elements: False True True
Comparison of Access Methods
| Method | Syntax | Use Case |
|---|---|---|
| Indexer | arr[index] = value |
Most common and readable approach |
| Get() method | arr.Get(index) |
Explicit method for retrieving bit values |
| Set() method | arr.Set(index, value) |
Explicit method for setting bit values |
Conclusion
The BitArray class provides flexible ways to access individual bits through both indexer syntax and explicit methods. The indexer approach arr[index] is more concise and commonly used, while the Get() and Set() methods offer explicit control over bit manipulation operations.
