Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
C# BitArray Class
3.
Constructors of the BitArray class
3.1.
Example
4.
Properties of the BitArray Class
4.1.
Example
5.
Methods of the BitArray Class
5.1.
Example
6.
Frequently Asked Questions
6.1.
What are the properties of a BitArray Class in C#?
6.2.
What is the main functioning of BitArray Class in C#?
6.3.
BitArray class is present in which namespace?
7.
Conclusion
Last Updated: Mar 27, 2024

C# BitArray Class

Author Sanjana Yadav
0 upvote

Introduction

The BitArray class keeps track of a small array of bit values, which are represented as Booleans, with true indicating that a bit is on (1) and false indicating that it is off (0).

It's utilized when you need to store bits but don't know how much you'll need ahead of time. The BitArray collection may be accessed using an integer index that starts at zero.

Recommended Topic, Palindrome in C#, singleton design pattern in c#, and Ienumerable vs Iqueryable

C# BitArray Class

The BitArray class is a collection type whose capacity and count are always the same.

By raising the Length attribute, elements are added to a BitArray.

By reducing the Length property, elements are eliminated.

An integer index can be used to retrieve elements in this collection. This collection's indexes are all zero-based.

Constructors of the BitArray class

BitArray(BitArray)

  • Creates a new BitArray class instance with bit values copied from the provided BitArray.

BitArray(Boolean[])

  • Creates a new instance of the BitArray class with bit values copied from the provided Boolean array.

BitArray(Byte[])

  • Creates a new BitArray instance with bit values copied from the provided array of bytes.

BitArray(Int32)

  • Creates a new BitArray object that can store the specified number of bit values initially set to false.

BitArray(Int32, Boolean)

  • Creates a new BitArray instance that can store the provided number of bit values initially set to the supplied value.

BitArray(Int32[])

  • Creates a new instance of the BitArray class with bit values copied from the provided 32-bit integer array.

Example

using System;
using System.Collections;
class ExampleCode{
    public static void Main()
    {
        // Creating a BitArray
        BitArray Bitarr = new BitArray(5);
        Bitarr[0] = false;
        Bitarr[1] = false;
        Bitarr[2] = true;
        Bitarr[3] = false;
        Bitarr[4] = true;
        Console.WriteLine(Bitarr.Get(2));
        Console.WriteLine(Bitarr.Get(3));
    }
}

Output

True
False

Properties of the BitArray Class

  • Count - Count the number of elements in the BitArray.
  • IsReadOnly - Check whether the BitArray is read-only or not.
  • Item - Gets or sets the bit value at a certain point in the BitArray.
  • Length - Gets and sets the number of elements in the BitArray.
  • SyncRoot - Gets an object that can be used to synchronize access to the BitArray.

Example

using System;
using System.Collections;
class ExampleProgram{
public static void Main()
{
BitArray arr = new BitArray(new byte[] {1, 2, 3, 4});
Console.WriteLine(arr.IsReadOnly);
Console.WriteLine(arr.Count);
}
}

Output

False

32 

Methods of the BitArray Class

  • public BitArray And(BitArray value): Compares the items in the current BitArray to the equivalent elements in the provided BitArray in a bitwise AND operation.
  • public bool Get(int index): Returns the value of the bit at a specific position in the BitArray.
  • public BitArray Not(): Inverts all the bit values in the current BitArray, converting true items to false and false elements to true.
  • public BitArray Or(BitArray value): Compares the items in the current BitArray to the equivalent elements in the provided BitArray in a bitwise OR operation.
  • public void Set(int index, bool value): Sets a bit in the BitArray to the provided value at a specific point.
  • public void SetAll(bool value): Sets the given value to all bits in the BitArray.
  • public BitArray Xor(BitArray value): Compares the elements in the current BitArray to the corresponding elements in the provided BitArray in a bitwise exclusive OR operation.

Example

using System;
using System.Collections;
class ExampleProgram{
public static void Main()
{
BitArray Arr1 = new BitArray(4);
BitArray Arr2 = new BitArray(4);
Arr1[0] = false;
Arr1[1] = false;
Arr1[2] = true;
Arr1[3] = true;
Arr2[0] = false;
Arr2[2] = false;
Arr2[1] = true;
Arr2[3] = true;
PrintValues(Arr1.Or(Arr2));
}
public static void PrintValues(IEnumerable myList)
{
foreach(Object obj in myList)
{
Console.WriteLine(obj);
}
}
}

Output

False
True
True
True

Must Read IEnumerable vs IQueryable.

Frequently Asked Questions

What are the properties of a BitArray Class in C#?

The BitArray class is a collection where the capacity and count are always equal. The Length attribute is used to add elements to a BitArray. The Length attribute is used to delete elements. An integer index can be used to access elements in this collection. This collection's indexes are all zero-based.

What is the main functioning of BitArray Class in C#?

The BitArray class keeps track of a compact array of bit values represented as Booleans, with true indicating that a bit is on and false indicating that it is off.

BitArray class is present in which namespace?

The namespace System.Collections contain this class.

Conclusion

In this article, we have extensively discussed the C# BitArray class. With the help of several examples, we learned different properties and methods of the BitArray class in C#.

We hope that this blog has helped you enhance your knowledge of the C# language, and if you would like to learn more about C#, check out our articles C# | Learn & Practice from Coding Ninjas Studio and C# interview questions for 5 years Experience.

Recommended problems -

 

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc., you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass