Set the bit at a specific position in the BitArray to the specified value in C#?

CsharpServer Side ProgrammingProgramming

To set the bit a specific position in the BitArray to the specified values, the code is as follows −

Example

 Live Demo

using System;
using System.Collections;
public class Demo {
   public static void Main() {
      BitArray arr = new BitArray(5);
      arr[0] = true;
      arr[1] = false;
      arr[2] = true;
      arr[3] = false;
      Console.WriteLine("BitArray...");
      foreach(Object ob in arr) {
         Console.WriteLine(ob);
      }
      arr.Set(2, false);
      Console.WriteLine("\nUpdated BitArray...");
      foreach(Object ob in arr) {
         Console.WriteLine(ob);
      }
   }
}

Output

This will produce the following output −

BitArray...
True
False
True
False
False
Updated BitArray...
True
False
False
False
False

Example

Let us now see another example −

 Live Demo

using System;
using System.Collections;
public class Demo {
   public static void Main() {
      BitArray arr = new BitArray(5);
      arr[0] = true;
      arr[1] = false;
      arr[2] = true;
      arr[3] = false;
      Console.WriteLine("BitArray...");
      foreach(Object ob in arr) {
         Console.WriteLine(ob);
      }
      arr.Set(0, false);
      arr.Set(1, true);
      arr.Set(2, false);
      Console.WriteLine("\nUpdated BitArray...");
      foreach(Object ob in arr) {
         Console.WriteLine(ob);
      }
   }
}

Output

This will produce the following output −

BitArray...
True
False
True
False
False
Updated BitArray...
False
True
False
False
False
raja
Published on 16-Dec-2019 09:23:57
Advertisements