ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

C# program to set the bit at a specific position in the BitArray to the specified value (BitArray.Set() Method)

C# BitArray.Set() Method: Here, we are going to learn how to set the bit at a specific position in the BitArray to the specified value in C#.Net?
Submitted by Nidhi, on May 04, 2021

The Set() method of BitArray class is used to set a specified boolean value at the given index of a BitArray object. The boolean value can be either True or False.

Syntax:

    void BitArray.Set(int index, bool value);

Parameter(s):

  • index: The index of the bit to set.
  • value: a Boolean value to be assigned to the bit.

Return value:

It does not return any value.

Exception(s):

  • System.ArgumentOutOfRangeException

Program:

The source code to set the bit at a specific position in the BitArray to the specified is given below. The given program is compiled and executed successfully.

using System;
using System.Collections;

class BitArrayEx
{
    //Entry point of Program
    static public void Main()
    {
        //Creation of BitArray objects
        BitArray bitArr = new BitArray(5);
        
        int index = 0;

        bitArr[0] = true;
        bitArr[1] = false;
        bitArr[2] = true;
        bitArr[3] = false;
        bitArr[4] = true;

        Console.WriteLine("Elements of BitArray before set() method:");
        for (index = 0; index < bitArr.Length; index++)
        {
            Console.WriteLine("\tIndex "+index + ": "+bitArr.Get(index));
        }

        bitArr.Set(0,false);
        bitArr.Set(1,true);

        Console.WriteLine("Elements of BitArray  after set() method:");
        for (index = 0; index < bitArr.Length; index++)
        {
            Console.WriteLine("\tIndex " + index + ": " + bitArr.Get(index));
        }
    }
}

Output:

Elements of BitArray before set() method:
        Index 0: True
        Index 1: False
        Index 2: True
        Index 3: False
        Index 4: True
Elements of BitArray  after set() method:
        Index 0: False
        Index 1: True
        Index 2: True
        Index 3: False
        Index 4: True
Press any key to continue . . .

C# BitArray Class Programs »

ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.