BitArray类管理一个紧凑的位值数组,这些值表示为布尔值,其中true表示该位打开,即1 ,false表示该位关闭,即0 。此类包含在System.Collections命名空间中。
BitArray类的属性:
- BitArray类是一个集合类,其中容量始终与计数相同。
- 通过增加Length属性将元素添加到BitArray中。
- 通过减小Length属性来删除元素。
- 可以使用整数索引访问此集合中的元素。此集合中的索引从零开始。
建设者
Constructor | Description |
---|---|
BitArray(BitArray) | Initializes a new instance of the BitArray class that contains bit values copied from the specified BitArray. |
BitArray(Boolean[]) | Initializes a new instance of the BitArray class that contains bit values copied from the specified array of Booleans. |
BitArray(Byte[]) | Initializes a new instance of the BitArray class that contains bit values copied from the specified array of bytes. |
BitArray(Int32) | Initializes a new instance of the BitArray class that can hold the specified number of bit values, which are initially set to false. |
BitArray(Int32, Boolean) | Initializes a new instance of the BitArray class that can hold the specified number of bit values, which are initially set to the specified value. |
BitArray(Int32[]) | Initializes a new instance of the BitArray class that contains bit values copied from the specified array of 32-bit integers. |
例子:
// C# code to create a BitArray
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating a BitArray
BitArray myBitArr = new BitArray(5);
myBitArr[0] = true;
myBitArr[1] = true;
myBitArr[2] = false;
myBitArr[3] = true;
myBitArr[4] = false;
// To get the value of index at index 2
Console.WriteLine(myBitArr.Get(2));
// To get the value of index at index 3
Console.WriteLine(myBitArr.Get(3));
}
}
输出:
False
True
特性
Property | Description |
---|---|
Count | Gets the number of elements contained in the BitArray. |
IsReadOnly | Gets a value indicating whether the BitArray is read-only. |
IsSynchronized | Gets a value indicating whether access to the BitArray is synchronized (thread safe). |
Item[Int32] | Gets or sets the value of the bit at a specific position in the BitArray. |
Length | Gets or sets the number of elements in the BitArray. |
SyncRoot | Gets an object that can be used to synchronize access to the BitArray. |
例子:
// C# program to illustrate the
// BitArray Class Properties
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating a BitArray
BitArray myBitArr = new BitArray(new byte[] { 0, 0, 0, 1 });
// -------- IsReadOnly Property --------
// Checking if the BitArray is read-only
Console.WriteLine(myBitArr.IsReadOnly);
// -------- Count Property --------
// To get the number of elements
// contained in the BitArray
Console.WriteLine(myBitArr.Count);
}
}
输出:
False
32
方法
Method | Description |
---|---|
And(BitArray) | Performs the bitwise AND operation between the elements of the current BitArray object and the corresponding elements in the specified array. The current BitArray object will be modified to store the result of the bitwise AND operation. |
Clone() | Creates a shallow copy of the BitArray. |
CopyTo(Array, Int32) | Copies the entire BitArray to a compatible one-dimensional Array, starting at the specified index of the target array. |
Equals(Object) | Determines whether the specified object is equal to the current object. |
Get(Int32) | Gets the value of the bit at a specific position in the BitArray. |
GetEnumerator() | Returns an enumerator that iterates through the BitArray. |
LeftShift(Int32) | It is used to shift the bits of the bit array to the left by one position and adds zeros on the shifted position. |
GetHashCode() | Serves as the default hash function. |
GetType() | Gets the Type of the current instance. |
MemberwiseClone() | Creates a shallow copy of the current Object. |
Not() | Inverts all the bit values in the current BitArray, so that elements set to true are changed to false, and elements set to false are changed to true. |
RightShift(Int32) | It is used to shift the bits of the bit array to the right by one position and adds zeros on the shifted position. |
Or(BitArray) | Performs the bitwise OR operation between the elements of the current BitArray object and the corresponding elements in the specified array. The current BitArray object will be modified to store the result of the bitwise OR operation. |
Set(Int32, Boolean) | Sets the bit at a specific position in the BitArray to the specified value. |
SetAll(Boolean) | Sets all bits in the BitArray to the specified value. |
ToString() | Returns a string that represents the current object. |
Xor(BitArray) | Performs the bitwise exclusive OR operation between the elements of the current BitArray object against the corresponding elements in the specified array. The current BitArray object will be modified to store the result of the bitwise exclusive OR operation. |
范例1:
// C# code to do bitwise
// OR between BitArray
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating a BitArray
BitArray myBitArr1 = new BitArray(4);
// Creating a BitArray
BitArray myBitArr2 = new BitArray(4);
// Initializing values in myBitArr1
myBitArr1[0] = false;
myBitArr1[1] = false;
myBitArr1[2] = true;
myBitArr1[3] = true;
// Initializing values in myBitArr2
myBitArr2[0] = false;
myBitArr2[2] = false;
myBitArr2[1] = true;
myBitArr2[3] = true;
// function calling
PrintValues(myBitArr1.Or(myBitArr2));
}
// Displaying the result
public static void PrintValues(IEnumerable myList)
{
foreach(Object obj in myList)
{
Console.WriteLine(obj);
}
}
}
输出:
False
True
True
True
范例2:
// C# code to set all bits in the
// BitArray to the specified value
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating a BitArray myBitArr
BitArray myBitArr = new BitArray(5);
// Initializing all the bits in myBitArr
myBitArr[0] = false;
myBitArr[1] = true;
myBitArr[2] = true;
myBitArr[3] = false;
myBitArr[4] = true;
// Printing the values in myBitArr
Console.WriteLine("Initially the bits are as : ");
PrintIndexAndValues(myBitArr);
// Setting all bits to false
myBitArr.SetAll(false);
// Printing the values in myBitArr
// It should display all the bits as false
Console.WriteLine("Finally the bits are as : ");
PrintIndexAndValues(myBitArr);
}
// Function to display bits
public static void PrintIndexAndValues(IEnumerable myArr)
{
foreach(Object obj in myArr)
{
Console.WriteLine(obj);
}
}
}
输出:
Initially the bits are as :
False
True
True
False
True
Finally the bits are as :
False
False
False
False
False
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.bitarray?view=netframework-4.7.2