Home »
.Net »
C# Programs
C# program to get the value of the bit at a specific position in the BitArray (BitArray.Get() Method)
C# BitArray.Get() Method: Here, we are going to learn how to get the value of the bit at a specific position in the BitArray in C#.Net?
Submitted by Nidhi, on May 03, 2021
The Get() method of BitArray class is used to get the value of the bit at a specific position in the BitArray.
Syntax:
bool BitArray.Get(int index);
Parameter(s):
- index: The index of the value to get.
Return value:
It returns a boolean value stored at the given index in the BitArray.
Exception(s):
- System.ArgumentOutOfRangeException
Program:
The source code to get the value of the bit at a specific position in the BitArray 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 object
BitArray bitArr = new BitArray(5);
int index = 0;
bitArr[0] = true;
bitArr[2] = true;
bitArr[3] = true;
Console.WriteLine("Elements of BitArray:");
for (index = 0; index < bitArr.Length; index++)
{
Console.WriteLine("\tIndex "+index + ": "+bitArr.Get(index));
}
}
}
Output:
Elements of BitArray:
Index 0: True
Index 1: False
Index 2: True
Index 3: True
Index 4: False
Press any key to continue . . .
C# BitArray Class Programs »