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.
The different constructors and their description is given as follows:
Table: Constructors in BitArray in C#
Source: MSDN
Constructors | Description |
---|---|
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. |
The different properties and their description is given as follows:
Table: Properties in BitArray in C#
Source: MSDN
Properties | Description |
---|---|
Count | This property gets the number of elements included in BitArray. |
IsReadOnly | This property gets a value that indicates whether BitArray is read-only. |
IsSynchronized | This 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 |
Length | This property gets or sets the number of BitArray elements. |
SyncRoot | This property gets an object that can be used to synchronize access to BitArray. |
The different methods and their description is given as follows:
Table: Methods in BitArray in C#
Source: MSDN
Methods | Description |
---|---|
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. |
Some of the BitArray operations are given as follows:
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
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
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
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
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
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
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
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
Avery good write-up. Please let me know what are the types of C# libraries used for AI development.
very satisfied!!
Good tutorial. Small question: Say, there is : enum numbers { one, two, three} and a string field_enum ="one" how would I from the variable field_enum have a response with value numbers.one so that it can be treated as an enum and not as a string. making a list from the enum, and loop into the list. is not elegant... and may not work is forced value on field is forced ( one = 100).
Hi Team Knowledge Hut, Thank you for such an informative post like this. I am completely new to this digital marketing field and do not have much idea about this, but your post has become a supportive pillar for me. After reading the blog I would expect to read more about the topic. I wish to get connected with you always to have updates on these sorts of ideas. Regards, Kshitiz
The reason abstraction can be used with this example is because, the triangle, circle. Square etc can be defined as a shape, for example.....shape c = new circle(5,0)...the abstract object c now points at the circle class. Thus hiding implementation
Leave a Reply
Your email address will not be published. Required fields are marked *