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


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("
Updated 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("
Updated 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

Updated on: 16-Dec-2019

126 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements