Home  >  Article  >  Backend Development  >  Negate all bit values ​​in BitArray in C#

Negate all bit values ​​in BitArray in C#

PHPz
PHPzforward
2023-08-24 14:05:02738browse

Negate 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.

Reverse all bit values ​​in the bit array

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

The Chinese translation of

Example 1

is:

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 value.

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

  • Step 3 − Use the bitwise negation operator (~) to invert the value of each bit.

  • Step 4 - Store the inverted value in a 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 above code output, this output corresponds to the inverted bit value of the original BitArray (1010).

0101
The Chinese translation of

Example 2

is:

Example 2

The following example demonstrates reversing 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 - Use the BitArray.Set method to invert the value at the current index. The parameters are the index and the inverse value of the current index.

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

in conclusion

In C#, inverting all bit values ​​in a BitArray is a simple task and can be accomplished using the XOR operator with the value 1. This technique is very useful when working with binary data and helps you manipulate individual bits in an array efficiently.

The above is the detailed content of Negate all bit values ​​in BitArray in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete