Inverting all bit values in BitArray in C#


A BitArray is a collection of Boolean values represented as a series of 1’s and 0’s. It is often used to store and manipulate binary data efficiently. In C#, the BitArray class is part of the System. Collections namespace, and it allows you to manipulate the individual bits in the array using bitwise operators.

Inverting all bit Values in a BitArray

To invert all bit values in a BitArray in C#, you can use the exclusive OR (XOR) operator (^) with the number 1. This operator returns a value of 1 if the bits being compared are different and 0 if they are the same. By applying this operator to each bit in the BitArray, you can invert all the bit values.

Example 1

The following example demonstrates how to invert all bit values in a BitArray in C#

Algorithm

  • Step 1 − Create a new BitArray to store the inverted values.

  • Step 2 − Loop through each bit in the original BitArray.

  • Step 3 − Invert the value of each bit using the bitwise NOT operator (~).

  • Step 4 − Store the inverted value in the new BitArray.

  • Step 5 − Return the new BitArray.

using System;
using System.Collections;
class Program{
   static void Main(string[] args){
      // Create a new BitArray with some initial values
      BitArray bits = new BitArray(new[] { true, false, true, false });

      // Invert all the bit values using XOR with 1
      for (int i = 0; i < bits.Length; i++){
         bits[i] ^= true;
      }

      // Print the inverted bit values
      for (int i = 0; i < bits.Length; i++){
         Console.Write(bits[i] ? "1" : "0");
      }
      Console.ReadLine();
   }
}

Output

When you run the code above output. This output corresponds to the inverted bit values of the original BitArray (1010).

0101

Example 2

The follwoing example demonstrates inverting all bit values in a BitArray in C#

Algorithm

  • Step 1 − Create a BitArray object with the desired size.

  • Step 2 − Loop through each index in the BitArray.

  • Step 3 − Invert the value at the current index using the BitArray.Set method with the index and the negated value of the current index as arguments.

using System;
using System.Collections;
class Program {
   static void Main(string[] args) {
      int size = 8;
      BitArray bits = new BitArray(size);
      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();
   }
}

Output

Before inversion:
01010101
After inversion:
10101010

Conclusion

Inverting all bit values in a BitArray in C# is a simple task that can be accomplished using the XOR operator with the value 1. This technique is useful when working with binary data and can help you manipulate the individual bits in the array efficiently.

Updated on: 25-Apr-2023

944 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements