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
Inverting all bit values in BitArray in C#
The BitArray class in C# is a collection of Boolean values represented as bits (1's and 0's). It is part of the System.Collections namespace and provides efficient storage and manipulation of binary data. One common operation is inverting all bit values in a BitArray.
Syntax
Following are the common methods to invert all bits in a BitArray
// Using built-in Not() method bitArray.Not(); // Using XOR with true bits[i] ^= true; // Using Set() method with negation bits.Set(i, !bits[i]);
Using the Built-in Not() Method
The simplest way to invert all bits in a BitArray is to use the built-in Not() method, which performs bitwise NOT operation on all elements
using System;
using System.Collections;
class Program {
static void Main(string[] args) {
// Create a BitArray with initial values
BitArray bits = new BitArray(new[] { true, false, true, false });
Console.WriteLine("Original BitArray:");
PrintBits(bits);
// Invert all bits using Not() method
bits.Not();
Console.WriteLine("After inversion:");
PrintBits(bits);
}
static void PrintBits(BitArray bits) {
for (int i = 0; i < bits.Count; i++) {
Console.Write(bits[i] ? "1" : "0");
}
Console.WriteLine();
}
}
The output of the above code is
Original BitArray: 1010 After inversion: 0101
Using XOR Operation
You can manually invert each bit using the XOR operator (^) with true. This approach gives you more control over the inversion process
using System;
using System.Collections;
class Program {
static void Main(string[] args) {
// Create a BitArray with some initial values
BitArray bits = new BitArray(new[] { true, false, true, false });
Console.WriteLine("Original BitArray:");
PrintBits(bits);
// Invert all bits using XOR with true
for (int i = 0; i < bits.Length; i++) {
bits[i] ^= true;
}
Console.WriteLine("After inversion:");
PrintBits(bits);
}
static void PrintBits(BitArray bits) {
for (int i = 0; i < bits.Count; i++) {
Console.Write(bits[i] ? "1" : "0");
}
Console.WriteLine();
}
}
The output of the above code is
Original BitArray: 1010 After inversion: 0101
Using Set() Method with Negation
Another approach is to use the Set() method combined with the logical NOT operator (!) to invert each bit individually
using System;
using System.Collections;
class Program {
static void Main(string[] args) {
int size = 8;
BitArray bits = new BitArray(size);
// Initialize with alternating pattern
for (int i = 0; i < size; i++) {
bits[i] = (i % 2 == 0);
}
Console.WriteLine("Before inversion:");
PrintBits(bits);
InvertBits(bits);
Console.WriteLine("After inversion:");
PrintBits(bits);
}
static void InvertBits(BitArray bits) {
for (int i = 0; i < bits.Count; i++) {
bits.Set(i, !bits[i]);
}
}
static void PrintBits(BitArray bits) {
for (int i = 0; i < bits.Count; i++) {
Console.Write(bits[i] ? "1" : "0");
}
Console.WriteLine();
}
}
The output of the above code is
Before inversion: 10101010 After inversion: 01010101
Performance Comparison
| Method | Performance | Use Case |
|---|---|---|
| Not() | Fastest | Simple inversion of entire BitArray |
| XOR (^=) | Medium | When you need custom logic during inversion |
| Set() with negation | Slowest | When you need explicit control over each bit |
Conclusion
Inverting all bit values in a BitArray can be accomplished using three main approaches: the built-in Not() method (fastest), XOR operations for custom control, or the Set() method for explicit bit manipulation. The Not() method is recommended for simple inversions due to its efficiency and simplicity.
