In C#, the BitArray class is used to manage a compact array of bit values. The values in the array are Boolean, where true (1) means that this bit is on and false (0) means that this bit is off.
When you need to store bits (the smallest unit of data storage in English name "bit" or bits), but don't know the specific number of bits in advance, you can use dot arrays. When you need to access elements in a point array, you can use an integer index to access the specified element from the point array, with the index starting from zero.
Properties in BitArray class
The following table lists some commonly used properties of the BitArray class:
property | describe |
---|---|
Count | Get the number of elements contained in the dot array |
IsReadOnly | Determine whether the dot array is read only |
Item | Gets or sets the value of the specified position in the dot array |
Length | Get or set the number of elements in the dot array |
Methods in BitArray class
The following table lists some commonly used methods of the BitArray class:
Method name | describe |
---|---|
public BitArray And(BitArray value) | Perform bitwise and operations on the elements in the current dot array and the corresponding elements in the specified dot array |
public bool Get(int index) | Get the bit value of the specified position in the dot array |
public BitArray Not() | Reverse the values of all bits in the current dot array, that is, set true to false and set false to true |
public BitArray Or(BitArray value) | Perform bitwise or operations on elements in the current dot array and corresponding elements in the specified dot array |
public void Set(int index, bool value) | Set the bits of the specified position in the dot array to the specified value |
public void SetAll(bool value) | Set all bits in the dot array to the specified value |
public BitArray Xor(BitArray value) | Perform bitwise exclusive OR on the elements in the current point array and the corresponding elements in the specified point array |
For a complete introduction to the properties and methods in the BitArray class, you can check it outC# official documentation。
[Example] The following example demonstrates the use of BitArray class properties and methods:
using System; using ; namespace { class Demo { static void Main(string[] args){ // Create two dot arrays of size 8 BitArray ba1 = new BitArray(8); BitArray ba2 = new BitArray(8); byte[] a = { 60 }; byte[] b = { 13 }; //Storage values 60 and 13 into the dot array ba1 = new BitArray(a); ba2 = new BitArray(b); // content of ba1 ("Dot array ba1: 60"); for (int i = 0; i < ; i++) { ("{0, -6} ", ba1[i]); } (); // content of ba2 ("Dot array ba2: 13"); for (int i = 0; i < ; i++) { ("{0, -6} ", ba2[i]); } (); BitArray ba3 = new BitArray(8); ba3 = (ba2); // Ba3 content ("Dot array after bitwise and operation is performed ba3:"); for (int i = 0; i < ; i++) { ("{0, -6} ", ba3[i]); } (); ba3 = (ba2); // Ba3 content ("Dot array after bitwise or operation is performed ba3:"); for (int i = 0; i < ; i++) { ("{0, -6} ", ba3[i]); } (); (); } } }
The operation results are as follows:
Dot array ba1: 60
False False True True True True False False
Dot array ba2: 13
True False True True False False False False
Dot array after bitwise and operation ba3:
False False True True False False False False
Dot array after bitwise or operation ba3:
True False True True False False False False
This is the end of this article about the use of C# BitArray dot array. For more related content of C# BitArray dot array, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!