The BitArray class in .Web 7 is a strong details framework that suppliers and manipulates bits of info. Every single factor in a BitArray can only keep a one bit ( or 1) represented as phony or correct, wherever bogus signifies the bit is off () and legitimate suggests the little bit is on (1). BitArrays can keep flags or successfully perform bitwise operations on data.

This report talks about utilizing BitArray in C# with relevant code illustrations wherever applicable. To perform with the code examples supplied in this post, you ought to have Visible Studio 2022 mounted in your method. If you never now have a duplicate, you can obtain Visible Studio 2022 listed here.

Produce a console software challenge in Visible Studio

First off, let us produce a .Internet Main console application job in Visual Studio. Assuming Visual Studio 2022 is mounted in your system, adhere to the actions outlined beneath to build a new .Web Core Console Software challenge in Visible Studio.

  1. Launch the Visible Studio IDE.
  2. Simply click on “Create new job.”
  3. In the “Create new project” window, choose “Console Application (.Internet Main)” from the checklist of templates exhibited.
  4. Simply click Up coming.
  5. In the “Configure your new project” window revealed future, specify the identify and site for the new job.
  6. Simply click Upcoming
  7. In the “Additional information” window shown next, decide on “.Web 7 (Preview)” as the Framework variation you would like to use.
  8. Click Build.

We’ll use this .Web 7 console software challenge to function with BitArray in the subsequent sections of this posting.

What is a BitArray?

A BitArray is a form contained in the Method.Collections namespace that signifies a compact array of little bit values. These values are expressed as boolean values, i.e., correct and fake. Here, the price correct implies the little bit is on, and the worth untrue signifies the little bit is off.

Simply because the BitArray course is positioned in the Procedure.Collections namespace, you will will need to include things like a utilizing directive for that namespace in your code. The BitArray class is declared in the Technique.Collections namespace as revealed down below.

general public sealed course BitArray : ICloneable, Procedure.Collections.ICollection

Generate a BitArray in .Net 7

You can build a BitArray of a specific dimension and fill it with all bogus values as demonstrated in the code snippet provided underneath.

var bitArray = new BitArray(10)

You can also go in a list of booleans to make a BitArray of a precise measurement and established the values.

var bitArray = new BitArray(new bool[] correct, fake, real)

When you have established your BitArray, you can entry and manipulate the individual bits applying the indexer. The indexer expects an integer and will return or set the value of that little bit.

bitArray[0] = real //sets the 1st bit to accurate
bitArray[1] = fake //sets the second bit to untrue
bitArray[0] //returns the benefit of the first bit (as a bool)

The following code snippet can be applied to build a BitArray, set values to its aspects, and then retrieve and exhibit the value of a distinct index in the BitArray.

BitArray bitArray = new BitArray(5)
bitArray[0] = real
bitArray[1] = false
bitArray[2] = genuine
bitArray[3] = untrue
bitArray[4] = bogus
Console.WriteLine(bitArray.Get(2))
Console.WriteLine(bitArray.Get(4))

When you execute the above piece of code, the values correct and untrue will be exhibited at the console window as demonstrated in Figure 1.

bitarray 01 IDG

Determine 1.

Manipulate bits in a BitArray

You can manipulate the bits in a BitArray possibly using its index or working with the Get and Set strategies of the BitArray course. To set or retrieve many bits from a BitArray, you can use the SetAll() and GetAll() strategies as proven in the code snippet offered below.

bitArray.SetAll(phony) //set all bits of the bit array to 
bitArray.Established(, real) //set very first little bit of the little bit array to 1
bitArray.Set(1, untrue) //established the 2nd bit of the little bit array to
bool end result = (bitArray[0] == 1) //confirm if to start with bit is equivalent to 1

Verify if a BitArray is read-only

If you need to test if a BitArray is ReadOnly, you can use the IsReadOnly home. This assets returns a Boolean benefit that implies no matter whether the BitArray is examine-only. The following code snippet displays how you can examine if a BitArray is browse-only.

BitArray bitArray = new BitArray(new byte[]  , 1, , 1,  )
Console.WriteLine(bitArray.IsReadOnly)

When you execute the previously mentioned piece of code, the textual content “False” will be displayed at the console window.

Size and Depend homes in a BitArray

The Duration house of a BitArray returns the range of bits in the array. The Rely home returns the count of the variety of correct and false values in the BitArray. Note that the Length home will normally return the whole quantity of bits in the array, even if all of them are wrong. In other words and phrases, the Duration and Rely qualities will show equivalent values for a BitArray.

The adhering to piece of code illustrates how you can get the Length and Depend of a BitArray.

var bitArray = new BitArray(new bool[]  true, phony, correct, untrue )
Console.WriteLine("Length: " + bitArray.Length)
Console.WriteLine("Depend: " + bitArray.Rely)

When you execute the over code, the output will be comparable to that revealed in Determine 2.

bitarray 02 IDG

Determine 2.

You may perhaps want to examine whether or not your BitArray instance is synchronized. This can be done by contacting the instance’s IsSynchronized assets, which will return accurate if the BitArray is synchronized and wrong usually.

Accomplish AND, OR, and NOT operations in a BitArray

The pursuing code listing shows how you can accomplish a bitwise AND procedure on two BitArray circumstances. A bitwise AND operation returns genuine (or 1) if both operands are legitimate, and returns phony otherwise. A bitwise OR procedure returns true if both or the two operands are genuine, and false or else.

var bitArray1 = new BitArray(new bool[]  correct, bogus, legitimate, bogus, accurate )
var bitArray2 = new BitArray(new bool[] accurate, wrong, accurate, legitimate, true )
bitArray1.Established(, genuine)
bitArray1.Established(1, false)
bitArray1.Established(2, accurate)
bitArray1.Set(3, genuine)
bitArray1.Set(4, phony)
bitArray2.Set(, accurate)
bitArray2.Set(1, genuine)
bitArray2.Established(2, false)
bitArray2.Established(3, real)
bitArray2.Set(4, phony)
bitArray1.And(bitArray2)
Console.WriteLine("Exhibiting the things of bitArray1 following AND procedure")
for (int i = i < bitArray1.Count i++)

    Console.Write(bitArray1[i] + "t")

When you execute the above code, the value of each element of bitArray1 will be displayed after the AND operation.

bitarray 03 IDG

Figure 3.

To perform a bitwise OR operation on two BitArrays, you can simply replace the AND operator with the OR operator in the preceding example. In other words, replace bitArray1.And(bitArray2) with bitArray1.Or(bitArray2).

var bitArray1 = new BitArray(new bool[]  true, false, true, false, true )
var bitArray2 = new BitArray(new bool[] true, false, true, true, true )
bitArray1.Set(0, true)
bitArray1.Set(1, false)
bitArray1.Set(2, true)
bitArray1.Set(3, true)
bitArray1.Set(4, false)
bitArray2.Set(0, true)
bitArray2.Set(1, true)
bitArray2.Set(2, false)
bitArray2.Set(3, true)
bitArray2.Set(4, false)
bitArray1.Or(bitArray2)
Console.WriteLine("Displaying the elements of bitArray1 after OR operation")
for (int i = 0 i < bitArray1.Count i++)

    Console.Write(bitArray1[i] + "t")

Performing a NOT operation on a BitArray will change all true elements to false and vice versa. The following code snippet would change the elements of bitArray1 from true, false, false, true, false to false, true, true, false, true .

bitArray1.Not()

Common uses for BitArrays

There are a number of common use cases for a BitArray, such as for performing bitwise operations to manipulate an image. The color of each pixel in an image is defined by a certain number of bits. Changing the color of a pixel requires manipulating the bits that comprise it. Using BitArray, it is easy to manipulate individual bits within an array.

BitArray is also commonly used when dealing with network packets. Packets contain a large amount of data, which may be formatted as bits or bytes depending on the protocol. You can easily extract and manipulate the bits contained in each packet with BitArray.

You can also use a BitArray to represent Boolean values in your application. By doing so, you can reduce your memory and storage requirements. A BitArray consumes 1/8th the space consumed by a bool because a BitArray stores only one bit for each value. Additionally, whereas a byte can hold only eight values and an integer can hold only 32, a BitArray can hold an arbitrary number of boolean values. If you are storing a massive amount of data, this difference can be quite significant.

Finally, when it comes to processing a huge collection, the advantages of BitArray and bitwise processing will become clear as soon as you start fetching data from the memory. For example, there will be a significant difference in performance between a BitArray of 10000 items and a List of 10000 items. There will be eight times more memory reads required for the List than for the BitArray.

Copyright © 2022 IDG Communications, Inc.

Leave a Reply