C# BitArray
BitArray is a collection in C# used to represent a set of bit values.
BitArray belongs to the System.Collections namespace and is mainly used for processing binary data or performing bit operations.
Compared with using a Boolean array (bool[]), BitArray is more efficient because it stores each bit in a compact form.
The BitArray class manages a compact array of bit values, represented as Booleans.
true indicates that a bit is on (1), and false indicates that a bit is off (0).
Use a BitArray when you need to store bits but do not know the number of bits in advance.
You can use theInteger IndexingItems in a BitArray collection are accessed by index, starting at zero.
Methods and Properties of the BitArray Class
The following table lists the BitArray Some commonly used members of the class property:
| Attribute name | Type | Description |
|---|---|---|
Count |
int |
Obtain the BitArray The number of bits in the BitArray (read-only). |
Length |
int |
Gets or sets the BitArray The number of bits in the BitArray (supports dynamic resizing). |
IsReadOnly |
bool |
Gets a value indicating whether BitArray is read-only (always false)。 |
SyncRoot |
object |
Gets an object used to synchronize access to the BitArray BitArray. |
IsSynchronized |
bool |
Gets a value indicating whether access to the BitArray BitArray is synchronized (thread-safe). |
Item[int index] |
bool |
Gets or sets the value of the bit at a specified index (accessed via the indexer). |
Property access:
BitArray bits = new BitArray(8); // 创建 8 位的 BitArray,所有位默认为 false
Console.WriteLine(bits.Length); // 输出:8
bits.Length = 10; // 修改长度为 10,新增的位默认为 false
The following table lists the BitArray Some commonly used members of the class Method:
| Method Name | Return Type | Description |
|---|---|---|
And(BitArray) |
BitArray |
Bitwise AND operation; performs an AND on the corresponding bits of two BitArray BitArray objects, modifying the current instance in place and returning the current instance itself. |
Or(BitArray) |
BitArray |
Bitwise OR operation; performs an OR on the corresponding bits of two BitArray BitArray objects, modifying the current instance in place and returning the current instance itself. |
Xor(BitArray) |
BitArray |
Bitwise XOR operation; performs an XOR on the corresponding bits of two BitArray BitArray objects, modifying the current instance in place and returning the current instance itself. |
Not() |
BitArray |
Bitwise NOT operation; inverts every bit of the current instance, modifying it in place and returning the current instance itself. |
Set(int index, bool value) |
void |
Sets the bit at the specified index to the specified value (true or false)。 |
SetAll(bool value) |
void |
Sets all bits to the specified value (true or false)。 |
Get(int index) |
bool |
Gets the value of the bit at the specified index. |
Clone() |
object |
Creates a shallow copy of the current BitArray BitArray (copy). |
CopyTo(Array array, int index) |
void |
data to the child component via BitArray Copies the contents of the BitArray to the specified array, starting at the specified index. |
Equals(object obj) |
bool |
Determines whether the specified object is equal to the current object. BitArray。 |
GetEnumerator() |
IEnumerator |
Returns an enumerator that iterates through the BitArray bits in the BitArray. |
Method usage:
BitArray bits = new BitArray(8, false); // 创建 8 位的 BitArray,初始值全部为 false
bits.Set(0, true); // 设置第 0 位为 true
Console.WriteLine(bits.Get(0)); // 输出:True
bits.SetAll(true); // 将所有位设置为 true
Example
The following example demonstrates the use of BitArray:
In BitArray,index 0 corresponds to the lowest bit of the binary number, so the bit string obtained by traversing in forward index order is the reverse of the everyday writing order; therefore the following example outputs by traversing in reverse order.
Methods such as And(), Or(), and Xor()modify the instance they are called on in placeand return themselves, so a copy constructor is used before each operation to
new BitArray(ba1)create a copy.
Comparison of Two Bit Orders for the Value 13
Everyday writing order (high bit first):
0
Bit 7
0
Bit 6
0
Bit 5
0
Bit 4
1
Bit 3
1
Bit 2
0
Bit 1
1
Bit 0
BitArray index order (index 0 corresponds to the lowest bit):
1
Index 0
0
Index 1
1
Index 2
1
Index 3
0
Index 4
0
Index 5
0
Index 6
0
Index 7
For the same value 13, traversing the BitArray in forward index order gives 1 0 1 1 0 0 0 0, which is exactly the reverse of the everyday writing order; therefore the example outputs by traversing in reverse order.
Example
using
System
;
using
System.Collections
;
namespace
CollectionsApplication
{
class
Program
{
static
void
Main
(
string
[
]
args
)
{
// Initialize two BitArrays to store the binary bits
BitArray ba1
=
new
BitArray
(
new
byte
[
]
{
60
}
)
;
// 60 = 00111100
BitArray ba2
=
new
BitArray
(
new
byte
[
]
{
13
}
)
;
// 13 = 00001101
// Output the contents of ba1 and ba2
Console
.
WriteLine
(
"Bit array ba1 (60):"
)
;
PrintBitArray
(
ba1
)
;
Console
.
WriteLine
(
"Bit array ba2 (13):"
)
;
PrintBitArray
(
ba2
)
;
// Perform the AND operation and output the result
// And/Or/Xor modify the instance they are called on in place, so create a copy of ba1 before each operation
BitArray baAnd
=
new
BitArray
(
ba1
)
;
baAnd
.
And
(
ba2
)
;
// 60 & 13 = 12
Console
.
WriteLine
(
"Bit array after AND operation (12):"
)
;
PrintBitArray
(
baAnd
)
;
// Perform the OR operation and output the result
BitArray baOr
=
new
BitArray
(
ba1
)
;
// Copy the original ba1 again
baOr
.
Or
(
ba2
)
;
// 60 | 13 = 61
Console
.
WriteLine
(
"Bit array after OR operation (61):"
)
;
PrintBitArray
(
baOr
)
;
// Perform the XOR operation and output the result
BitArray baXor
=
new
BitArray
(
ba1
)
;
// Copy the original ba1 again
baXor
.
Xor
(
ba2
)
;
// 60 ^ 13 = 49
Console
.
WriteLine
(
"Bit array after XOR operation (49):"
)
;
PrintBitArray
(
baXor
)
;
Console
.
ReadKey
(
)
;
}
// Traverse in reverse order so the output follows the 'high bit first' writing convention
static
void
PrintBitArray
(
BitArray bitArray
)
{
for
(
int
i
=
bitArray
.
Length
1
;
i
=
0
;
i
--
)
{
Console
.
Write
(
$
"{(bitArray[i] ? 1 : 0)} "
)
;
}
Console
.
WriteLine
(
"\n"
)
;
}
}
}
When the code above is compiled and executed, it produces the following result:
Bit array ba1 (60):
0 0 1 1 1 1 0 0
Bit array ba2 (13):
0 0 0 0 1 1 0 1
Bit array after AND operation (12):
0 0 0 0 1 1 0 0
Bit array after OR operation (61):
0 0 1 1 1 1 0 1
Bit array after XOR operation (49):
0 0 1 1 0 0 0 1
C# Collections