top

Search

C# Tutorial

The BitArray collection handles a compact matrix of bit values. These bit values are represented as boolean values where true indicates 1 and false indicates 0.Constructors in BitArray CollectionThe different constructors and their description is given as follows:Table: Constructors in BitArray in C#Source: MSDNConstructorsDescriptionBitArray (BitArray)This constructor initializes a new instance of the BitArray class that contains the bit values copied from the specified BitArray collection.BitArray (Boolean [])This constructor initializes a new instance of the BitArray class that contains the bit values copied from the specified Boolean array.BitArray (Byte [])This constructor initializes a new instance of the BitArray class that contains the bit values copied from the specified byte array.BitArray (Int32)This constructor initializes a new instance of the BitArray class that can contain the specified number of bit values, initially set to false.BitArray (Int32, Boolean)This constructor initializes a new instance of the BitArray class that can contain the specified number of bit values, initially set to the specified value.BitArray (Int32 [])This constructor initializes a new instance of the BitArray class that contains the bit values copied from the specified 32-bit integer array.Properties in BitArray CollectionThe different properties and their description is given as follows:Table: Properties in BitArray in C#Source: MSDNPropertiesDescriptionCountThis property gets the number of elements included in BitArray.IsReadOnlyThis property gets a value that indicates whether BitArray is read-only.IsSynchronizedThis property gets a value that indicates whether access to the BitArray interface is synchronized (it is thread safe).Item [Int32]This property gets or sets the value of the bit that is in a specific BitArray positionLengthThis property gets or sets the number of BitArray elements.SyncRootThis property gets an object that can be used to synchronize access to BitArray.Methods in BitArray CollectionThe different methods and their description is given as follows:Table: Methods in BitArray in C#Source: MSDNMethodsDescriptionAnd(Bit Array)This method performs the AND operation bit by bit between the elements of the current BitArray object and the corresponding elements of the specified array. The current BitArray object will be modified to store the result of the bitwise AND operationClone()This method creates a surface copy of the BitArray collection.CopyTo( Array, Int32)This method copies the entire BitArray into a compatible one-dimensional array, starting at the specified index of the target array.Equals(Object)This method determines whether the specified object is equal to the current object.Get(Int32)This method gets the value of the bit in a specific BitArray positionGetEnumerator()This method returns an enumerator that iterates through the BitArray collection.GetHashCode()This method serves as the default hash function.GetType()This method gets the Type of the current instance.MemberwiseClone()This method creates a surface copy of the current Object.Not()This method inverts all the bit values that are in the current BitArray collection, so that the elements set in true change to false and the elements set in false change toa true.Or(Bit Array)This method performs the bitwise OR operation between the elements of the current BitArrayobject and the corresponding elements of the specified array. The current BitArray object will be modified to store the result of the bitwise OR operation.Set(Int32, Boolean)This method sets the bit located at a specific BitArray position to the specified valueSetAll (Boolean)This method sets all BitArray bits to the specified value.ToString()This method returns a string that represents the current object.Xor(Bit Array)This method performs the exclusive OR bitwise operation between the elements of the current BitArray object against the corresponding elements of the specified array. The current BitArray object will be modified to store the result of the bitwise exclusive OR operation.BitArray OperationsSome of the BitArray operations are given as follows:Create a BitArrayA BitArray can be created using new. Then the appropriate boolean values can be inserted into a BitArray. The program that demonstrates this is given as follows:Source Code: Program to create BitArray in C#using System; using System.Collections; namespace BitArrayDemo {   class Example   {      static void Main(string[] args)      {        BitArray bArr = new BitArray(5);        bArr.Set(0, true);        bArr.Set(1, false);        bArr.Set(2, true);        bArr.Set(3, true);        bArr.Set(4, false);        for (int i = 0; i < bArr.Count; i++)        {            Console.Write(bArr [i]);            Console.Write(" ");        }      }   } }The output of the above program is as follows:True False True True FalseAND Operation in BitArrayThe And() method performs the AND operation bit by bit between the elements of the current BitArray and the corresponding elements of the BitArray that is specified.  The program that demonstrates this is given as follows:Source Code: Program to demonstrate AND operation in BitArray in C#using System; using System.Collections; namespace BitArrayDemo {   class Example   {      static void Main(string[] args)      {        BitArray bArr1 = new BitArray(5);        BitArray bArr2 = new BitArray(5);        bArr1.Set(0, true);        bArr1.Set(1, false);        bArr1.Set(2, true);        bArr1.Set(3, true);        bArr1.Set(4, false);        bArr2.Set(0, true);        bArr2.Set(1, true);        bArr2.Set(2, false);        bArr2.Set(3, true);        bArr2.Set(4, false);        Console.WriteLine("BitArray 1");        for (int i = 0; i < bArr1.Count; i++)        {            Console.Write(bArr1[i]);            Console.Write(" ");        }        Console.WriteLine();        Console.WriteLine("BitArray 2");        for (int i = 0; i < bArr2.Count; i++)        {            Console.Write(bArr2[i]);            Console.Write(" ");        }        bArr1.And(bArr2);        Console.WriteLine();        Console.WriteLine("BitArray after AND operation");        for (int i = 0; i < bArr1.Count; i++)        {            Console.Write(bArr1[i]);            Console.Write(" ");              }      }   } }The output of the above program is as follows:BitArray 1 True False True True False BitArray 2 True True False True False BitArray after AND operation True False False True FalseOR Operation in BitArrayThe Or() method performs the OR operation bit by bit between the elements of the current BitArray and the corresponding elements of the BitArray that is specified.  The program that demonstrates this is given as follows:Source Code: Program to demonstrate OR operation in BitArray in C#using System; using System.Collections; namespace BitArrayDemo {   class Example   {      static void Main(string[] args)      {        BitArray barr1 = new BitArray(5);        BitArray barr2 = new BitArray(5);        barr1.Set(0, true);        barr1.Set(1, false);        barr1.Set(2, true);        barr1.Set(3, true);        barr1.Set(4, false);        barr2.Set(0, true);        barr2.Set(1, true);        barr2.Set(2, false);        barr2.Set(3, true);        barr2.Set(4, false);        Console.WriteLine("BitArray 1");        for (int i = 0; i < barr1.Count; i++)        {            Console.Write(barr1[i]);            Console.Write(" ");        }        Console.WriteLine();        Console.WriteLine("BitArray 2");        for (int i = 0; i < barr2.Count; i++)        {            Console.Write(barr2[i]);            Console.Write(" ");        }        barr1.Or(barr2);        Console.WriteLine();        Console.WriteLine("BitArray after OR operation");        for (int i = 0; i < barr1.Count; i++)        {            Console.Write(barr1[i]);            Console.Write(" ");           }        }   } }The output of the above program is as follows:BitArray 1 True False True True False BitArray 2 True True False True False BitArray after OR operation True True True True FalseNOT Operation in BitArrayThe Not() method inverts all the elements in the specified BitArray so the true elements are converted to false and the false elements are converted to true. The program that demonstrates this is given as follows:Source Code: Program to demonstrate NOT operation in BitArray in C#using System; using System.Collections; namespace BitArrayDemo {   class Example   {      static void Main(string[] args)      {        BitArray barr = new BitArray(5);        barr.Set(0, true);        barr.Set(1, false);        barr.Set(2, true);        barr.Set(3, true);        barr.Set(4, false);        Console.WriteLine("BitArray");        for (int i = 0; i < barr.Count; i++)        {            Console.Write(barr[i]);            Console.Write(" ");                    }        barr.Not();        Console.WriteLine();        Console.WriteLine("BitArray after NOT operation");        for (int i = 0; i < barr.Count; i++)        {            Console.Write(barr[i]);            Console.Write(" ");        }      }   } }The output of the above program is as follows:BitArray True False True True False BitArray after NOT operation False True False False TrueCheck whether the BitArray is ReadOnly or notUse the IsReadOnly property to check whether the BitArray is ReadOnly or not. If a collection is ReadOnly, then you won’t be able to add new elements to it.Let us see an example that checks whether the BitArray is ReadOnly or not:Source Code: Program to check whether the BitArray is ReadOnly or not in C#using System; using System.Collections; namespace BitArrayDemo {   class Example   {      static void Main(string[] args)      {        BitArray bArr = new BitArray(5);        bArr.Set(0, true);        bArr.Set(1, false);        bArr.Set(2, true);        bArr.Set(3, true);        for (int i = 0; i < bArr.Count; i++)        {            Console.Write(bArr [i]);            Console.Write(" ");        }        Console.WriteLine("\nIsReadOnly? " + bArr.IsReadOnly);      }   } }The above program gives the following output:True False True True False IsReadOnly? FalseGet the length of the elements in a BitArrayTo get the length of the BitArray elements, use the Length property. Let us see an example:Source Code: Program to get the length of the BitArray elements in C#using System; using System.Collections; public class Example  {   public static void Main()  {      BitArray bArr1 = new BitArray(15);      BitArray bArr2 = new BitArray(5);        bArr2.Set(0, true);        bArr2.Set(1, false);        bArr2.Set(2, true);        bArr2.Set(3, true);        bArr2.Set(4, false);        for (int i = 0; i < bArr2.Count; i++)        {            Console.Write(bArr2 [i]);            Console.Write(" ");        }      Console.WriteLine( "\nLength: {0}", bArr1.Length );      Console.WriteLine( "Length: {0}", bArr2.Length );   } }The above program gives the following output:True False True True False Length: 15 Length: 5Get the count of the elements in a BitArrayTo get the count of the elements in  BitArray, use the Count property. Let us see an example:Source Code: Program to get the count of the elements in  BitArray in C# using System; using System.Collections; public class Demo  {   public static void Main()  {      BitArray bArr1 = new BitArray( 10 );      BitArray bArr2 = new BitArray(5);        bArr2.Set(0, true);        bArr2.Set(1, false);        bArr2.Set(2, true);        bArr2.Set(3, true);        bArr2.Set(4, false);        for (int i = 0; i < bArr2.Count; i++)        {            Console.Write(bArr2 [i]);            Console.Write(" ");        }      Console.WriteLine( "\nCount: {0}", bArr1.Count );      Console.WriteLine( "Count: {0}", bArr2.Count );   } }The above program gives the following output:True False True True False Count: 10 Count: 5Fetch a value from a BitArray CollectionIn a BitArray collection, you may need to fetch a value. For that, the following is the code. Here, we are displaying a value from the BitArray elements:Source Code: Program to fetch a value from a BitArray collection in C#using System; using System.Collections; public class Example  {   public static void Main()  {      BitArray bArr1 = new BitArray(15);      BitArray bArr2 = new BitArray(5);        bArr2.Set(0, true);        bArr2.Set(1, false);        bArr2.Set(2, true);        bArr2.Set(3, true);        bArr2.Set(4, false);        for (int i = 0; i < bArr2.Count; i++)        {            Console.Write(bArr2 [i]);            Console.Write(" ");        }      Console.WriteLine( "\nLength: {0}", bArr1.Length );      Console.WriteLine( "Length: {0}", bArr2.Length );      bool s = bArr2[3];      Console.WriteLine("4th element ="+s);   } }The above program generated the following output:True False True True False Length: 15 Length: 5 4th element =True
logo

C# Tutorial

BitArray Collection in C#

The BitArray collection handles a compact matrix of bit values. These bit values are represented as boolean values where true indicates 1 and false indicates 0.

Constructors in BitArray Collection

The different constructors and their description is given as follows:

Table: Constructors in BitArray in C#

Source: MSDN

ConstructorsDescription
BitArray (BitArray)This constructor initializes a new instance of the BitArray class that contains the bit values copied from the specified BitArray collection.
BitArray (Boolean [])This constructor initializes a new instance of the BitArray class that contains the bit values copied from the specified Boolean array.
BitArray (Byte [])This constructor initializes a new instance of the BitArray class that contains the bit values copied from the specified byte array.
BitArray (Int32)This constructor initializes a new instance of the BitArray class that can contain the specified number of bit values, initially set to false.
BitArray (Int32, Boolean)This constructor initializes a new instance of the BitArray class that can contain the specified number of bit values, initially set to the specified value.
BitArray (Int32 [])
This constructor initializes a new instance of the BitArray class that contains the bit values copied from the specified 32-bit integer array.

Properties in BitArray Collection

The different properties and their description is given as follows:

Table: Properties in BitArray in C#

Source: MSDN

PropertiesDescription
CountThis property gets the number of elements included in BitArray.
IsReadOnlyThis property gets a value that indicates whether BitArray is read-only.
IsSynchronizedThis property gets a value that indicates whether access to the BitArray interface is synchronized (it is thread safe).
Item [Int32]This property gets or sets the value of the bit that is in a specific BitArray position
LengthThis property gets or sets the number of BitArray elements.
SyncRootThis property gets an object that can be used to synchronize access to BitArray.

Methods in BitArray Collection

The different methods and their description is given as follows:

Table: Methods in BitArray in C#

Source: MSDN

MethodsDescription
And(Bit Array)This method performs the AND operation bit by bit between the elements of the current BitArray object and the corresponding elements of the specified array. The current BitArray object will be modified to store the result of the bitwise AND operation
Clone()This method creates a surface copy of the BitArray collection.
CopyTo( Array, Int32)This method copies the entire BitArray into a compatible one-dimensional array, starting at the specified index of the target array.
Equals(Object)This method determines whether the specified object is equal to the current object.
Get(Int32)This method gets the value of the bit in a specific BitArray position
GetEnumerator()This method returns an enumerator that iterates through the BitArray collection.
GetHashCode()This method serves as the default hash function.
GetType()This method gets the Type of the current instance.
MemberwiseClone()This method creates a surface copy of the current Object.
Not()This method inverts all the bit values that are in the current BitArray collection, so that the elements set in true change to false and the elements set in false change toa true.
Or(Bit Array)This method performs the bitwise OR operation between the elements of the current BitArrayobject and the corresponding elements of the specified array. The current BitArray object will be modified to store the result of the bitwise OR operation.
Set(Int32, Boolean)This method sets the bit located at a specific BitArray position to the specified value
SetAll (Boolean)This method sets all BitArray bits to the specified value.
ToString()This method returns a string that represents the current object.
Xor(Bit Array)This method performs the exclusive OR bitwise operation between the elements of the current BitArray object against the corresponding elements of the specified array. The current BitArray object will be modified to store the result of the bitwise exclusive OR operation.

BitArray Operations

Some of the BitArray operations are given as follows:

Create a BitArray

A BitArray can be created using new. Then the appropriate boolean values can be inserted into a BitArray. The program that demonstrates this is given as follows:

Source Code: Program to create BitArray in C#

using System;
using System.Collections;
namespace BitArrayDemo
{
  class Example
  {
     static void Main(string[] args)
     {
       BitArray bArr = new BitArray(5);
       bArr.Set(0, true);
       bArr.Set(1, false);
       bArr.Set(2, true);
       bArr.Set(3, true);
       bArr.Set(4, false);
       for (int i = 0; i < bArr.Count; i++)
       {
           Console.Write(bArr [i]);
           Console.Write(" ");
       }
     }
  }
}

The output of the above program is as follows:

True False True True False

AND Operation in BitArray

The And() method performs the AND operation bit by bit between the elements of the current BitArray and the corresponding elements of the BitArray that is specified.  The program that demonstrates this is given as follows:

Source Code: Program to demonstrate AND operation in BitArray in C#

using System;
using System.Collections;
namespace BitArrayDemo
{
  class Example
  {
     static void Main(string[] args)
     {
       BitArray bArr1 = new BitArray(5);
       BitArray bArr2 = new BitArray(5);
       bArr1.Set(0, true);
       bArr1.Set(1, false);
       bArr1.Set(2, true);
       bArr1.Set(3, true);
       bArr1.Set(4, false);
       bArr2.Set(0, true);
       bArr2.Set(1, true);
       bArr2.Set(2, false);
       bArr2.Set(3, true);
       bArr2.Set(4, false);
       Console.WriteLine("BitArray 1");
       for (int i = 0; i < bArr1.Count; i++)
       {
           Console.Write(bArr1[i]);
           Console.Write(" ");
       }
       Console.WriteLine();
       Console.WriteLine("BitArray 2");
       for (int i = 0; i < bArr2.Count; i++)
       {
           Console.Write(bArr2[i]);
           Console.Write(" ");
       }
       bArr1.And(bArr2);
       Console.WriteLine();
       Console.WriteLine("BitArray after AND operation");
       for (int i = 0; i < bArr1.Count; i++)
       {
           Console.Write(bArr1[i]);
           Console.Write(" ");      
       }
     }
  }
}

The output of the above program is as follows:

BitArray 1
True False True True False
BitArray 2
True True False True False
BitArray after AND operation
True False False True False

OR Operation in BitArray

The Or() method performs the OR operation bit by bit between the elements of the current BitArray and the corresponding elements of the BitArray that is specified.  The program that demonstrates this is given as follows:

Source Code: Program to demonstrate OR operation in BitArray in C#

using System;
using System.Collections;
namespace BitArrayDemo
{
  class Example
  {
     static void Main(string[] args)
     {
       BitArray barr1 = new BitArray(5);
       BitArray barr2 = new BitArray(5);
       barr1.Set(0, true);
       barr1.Set(1, false);
       barr1.Set(2, true);
       barr1.Set(3, true);
       barr1.Set(4, false);
       barr2.Set(0, true);
       barr2.Set(1, true);
       barr2.Set(2, false);
       barr2.Set(3, true);
       barr2.Set(4, false);
       Console.WriteLine("BitArray 1");
       for (int i = 0; i < barr1.Count; i++)
       {
           Console.Write(barr1[i]);
           Console.Write(" ");
       }
       Console.WriteLine();
       Console.WriteLine("BitArray 2");
       for (int i = 0; i < barr2.Count; i++)
       {
           Console.Write(barr2[i]);
           Console.Write(" ");
       }
       barr1.Or(barr2);
       Console.WriteLine();
       Console.WriteLine("BitArray after OR operation");
       for (int i = 0; i < barr1.Count; i++)
       {
           Console.Write(barr1[i]);
           Console.Write(" ");   
       }  
     }
  }
}

The output of the above program is as follows:

BitArray 1
True False True True False
BitArray 2
True True False True False
BitArray after OR operation
True True True True False

NOT Operation in BitArray

The Not() method inverts all the elements in the specified BitArray so the true elements are converted to false and the false elements are converted to true. The program that demonstrates this is given as follows:

Source Code: Program to demonstrate NOT operation in BitArray in C#

using System;
using System.Collections;
namespace BitArrayDemo
{
  class Example
  {
     static void Main(string[] args)
     {
       BitArray barr = new BitArray(5);
       barr.Set(0, true);
       barr.Set(1, false);
       barr.Set(2, true);
       barr.Set(3, true);
       barr.Set(4, false);
       Console.WriteLine("BitArray");
       for (int i = 0; i < barr.Count; i++)
       {
           Console.Write(barr[i]);
           Console.Write(" ");            
       }
       barr.Not();
       Console.WriteLine();
       Console.WriteLine("BitArray after NOT operation");
       for (int i = 0; i < barr.Count; i++)
       {
           Console.Write(barr[i]);
           Console.Write(" ");
       }
     }
  }
}

The output of the above program is as follows:

BitArray
True False True True False
BitArray after NOT operation
False True False False True

Check whether the BitArray is ReadOnly or not

Use the IsReadOnly property to check whether the BitArray is ReadOnly or not. If a collection is ReadOnly, then you won’t be able to add new elements to it.

Let us see an example that checks whether the BitArray is ReadOnly or not:

Source Code: Program to check whether the BitArray is ReadOnly or not in C#

using System;
using System.Collections;
namespace BitArrayDemo
{
  class Example
  {
     static void Main(string[] args)
     {
       BitArray bArr = new BitArray(5);
       bArr.Set(0, true);
       bArr.Set(1, false);
       bArr.Set(2, true);
       bArr.Set(3, true);
       for (int i = 0; i < bArr.Count; i++)
       {
           Console.Write(bArr [i]);
           Console.Write(" ");
       }
       Console.WriteLine("\nIsReadOnly? " + bArr.IsReadOnly);
     }
  }
}

The above program gives the following output:

True False True True False
IsReadOnly? False

Get the length of the elements in a BitArray

To get the length of the BitArray elements, use the Length property. Let us see an example:

Source Code: Program to get the length of the BitArray elements in C#

using System;
using System.Collections;
public class Example  {
  public static void Main()  {
     BitArray bArr1 = new BitArray(15);
     BitArray bArr2 = new BitArray(5);
       bArr2.Set(0, true);
       bArr2.Set(1, false);
       bArr2.Set(2, true);
       bArr2.Set(3, true);
       bArr2.Set(4, false);
       for (int i = 0; i < bArr2.Count; i++)
       {
           Console.Write(bArr2 [i]);
           Console.Write(" ");
       }
     Console.WriteLine( "\nLength: {0}", bArr1.Length );
     Console.WriteLine( "Length: {0}", bArr2.Length );
  }
}

The above program gives the following output:

True False True True False
Length: 15
Length: 5

Get the count of the elements in a BitArray

To get the count of the elements in  BitArray, use the Count property. Let us see an example:

Source Code: Program to get the count of the elements in  BitArray in C# 

using System;
using System.Collections;
public class Demo  {
  public static void Main()  {
     BitArray bArr1 = new BitArray( 10 );
     BitArray bArr2 = new BitArray(5);
       bArr2.Set(0, true);
       bArr2.Set(1, false);
       bArr2.Set(2, true);
       bArr2.Set(3, true);
       bArr2.Set(4, false);
       for (int i = 0; i < bArr2.Count; i++)
       {
           Console.Write(bArr2 [i]);
           Console.Write(" ");
       }
     Console.WriteLine( "\nCount: {0}", bArr1.Count );
     Console.WriteLine( "Count: {0}", bArr2.Count );
  }
}

The above program gives the following output:

True False True True False
Count: 10
Count: 5

Fetch a value from a BitArray Collection

In a BitArray collection, you may need to fetch a value. For that, the following is the code. Here, we are displaying a value from the BitArray elements:

Source Code: Program to fetch a value from a BitArray collection in C#

using System;
using System.Collections;
public class Example  {
  public static void Main()  {
     BitArray bArr1 = new BitArray(15);
     BitArray bArr2 = new BitArray(5);
       bArr2.Set(0, true);
       bArr2.Set(1, false);
       bArr2.Set(2, true);
       bArr2.Set(3, true);
       bArr2.Set(4, false);
       for (int i = 0; i < bArr2.Count; i++)
       {
           Console.Write(bArr2 [i]);
           Console.Write(" ");
       }
     Console.WriteLine( "\nLength: {0}", bArr1.Length );
     Console.WriteLine( "Length: {0}", bArr2.Length );
     bool s = bArr2[3];
     Console.WriteLine("4th element ="+s);
  }
}

The above program generated the following output:

True False True True False
Length: 15
Length: 5
4th element =True

Leave a Reply

Your email address will not be published. Required fields are marked *

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

Python Tutorial

Python Tutorial