Set all bits in the BitArray to the specified value in C#


To set all bits in the BitArray to the specified value, 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] = false;
      arr[1] = false;
      arr[2] = false;
      arr[3] = false;
      Console.WriteLine("BitArray...");
      foreach(Object ob in arr) {
         Console.WriteLine(ob);
      }
      arr.SetAll(true);
      Console.WriteLine("
Updated BitArray...");       foreach(Object ob in arr) {          Console.WriteLine(ob);       }    } }

Output

This will produce the following output −

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

Example

Let us 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.SetAll(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
False
False
False
False

Updated on: 11-Dec-2019

50 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements