BitArray และ Property Count
BitArray (ใน System.Collections) จัดการ Array ของ Bit (Boolean) ใช้ Property Count ดึงจำนวน Elements (Bits)
ตัวอย่างที่ 1: BitArray พื้นฐาน และ Bitwise OR
using System;
using System.Collections;
public class Demo
{
public static void Main()
{
BitArray arr1 = new BitArray(5); // 5 bits, default false
BitArray arr2 = new BitArray(5);
arr1[0] = false; arr1[1] = false;
arr2[0] = false; arr2[1] = true;
Console.WriteLine("BitArray1 elements...");
foreach (bool bit in arr1) Console.WriteLine(bit);
Console.WriteLine("\nBitArray2 elements...");
foreach (bool bit in arr2) Console.WriteLine(bit);
// Bitwise OR
Console.WriteLine("\nBitwise OR operation...");
BitArray result = arr1.Or(arr2); // หรือ arr1 | arr2 (operator)
foreach (bool bit in result) Console.WriteLine(bit);
// Count
Console.WriteLine($"\nNumber of elements in resultant BitArray = {result.Count}"); // 5
}
}
ผลลัพธ์:
BitArray1 elements... False False False False False BitArray2 elements... False True False False False Bitwise OR operation... False True False False False Number of elements in resultant BitArray = 5
ตัวอย่างที่ 2: CopyTo และ Count
using System;
using System.Collections;
public class Demo
{
public static void Main()
{
BitArray arr = new BitArray(2);
arr[0] = false;
arr[1] = true;
Console.WriteLine("Elements in BitArray...");
foreach (bool bit in arr) Console.WriteLine(bit);
Console.WriteLine($"\nNumber of elements in the BitArray = {arr.Count}"); // 2
// Copy ไป Array
bool[] boolArr = new bool[2];
arr.CopyTo(boolArr, 0);
Console.WriteLine("\nArray...");
foreach (bool b in boolArr) Console.WriteLine(b);
}
}
ผลลัพธ์:
Elements in BitArray... False True Number of elements in the BitArray = 2 Array... False True
BitArray Constructor Overloads
// 1. จำนวน Bits (default false)
var bits1 = new BitArray(100);
// 2. จาก Boolean Array
bool[] boolValues = { true, false, true };
var bits2 = new BitArray(boolValues);
// 3. จาก Byte Array (แต่ละ Byte = 8 Bits)
byte[] bytes = { 0xFF, 0x0F }; // 11111111 00001111
var bits3 = new BitArray(bytes); // 16 bits
// 4. จาก Int Array (แต่ละ Int = 32 Bits)
int[] ints = { 0xFFFFFFFF, 0x0000FFFF };
var bits4 = new BitArray(ints); // 64 bits
// 5. ความยาว + ค่าเริ่มต้น
var bits5 = new BitArray(10, true); // 10 bits ทั้งหมด true
Property และ Method สำคัญ
| Member | คำอธิบาย |
|---|---|
| Count | จำนวน Bits (Read-only) |
| Length | Alias ของ Count (Read-only) |
| this[int index] | Get/Set Bit ที่ Index (Indexer) |
| Get(int index) | Get Bit Value |
| Set(int index, bool value) | Set Bit Value |
| SetAll(bool value) | Set ทุก Bit ให้เป็นค่าเดียวกัน |
| And/Or/Xor/Not | Bitwise Operations (Mutates Current Instance) |
| CopyTo(Array, int) | Copy Bits ไป Array |
| Clone() | Shallow Copy BitArray ใหม่ |
Bitwise Operations
BitArray a = new BitArray(new[] { true, false, true, false });
BitArray b = new BitArray(new[] { false, true, true, false });
// Mutate a (in-place)
a.And(b); // a = a & b -> false, false, true, false
a.Or(b); // a = a | b
a.Xor(b); // a = a ^ b
a.Not(); // a = !a (Invert all bits)
// Non-mutating (สร้างใหม่) - ใช้ Extension Method หรือ Manual
var andResult = new BitArray(a).And(b); // ต้อง Clone ก่อน
Performance & Memory
- Memory Efficient: 1 Bit ต่อ Element (vs bool[] ใช้ 1 Byte ต่อ Element)
- 1000 Bits ≈ 125 Bytes vs bool[1000] ≈ 1000 Bytes
- เหมาะสำหรับ: Flags, Bitmasks, Large Boolean Sets, Bloom Filters
- ไม่ใช่ Thread-Safe - ต้อง Lock เองถ้า Multi-thread
สรุป
BitArray.CountหรือBitArray.Length- ดึงจำนวน Bits- Fixed Size - สร้างด้วย Capacity แล้วไม่เปลี่ยนแปลงได้ (ต้อง New ใหม่)
- Indexer
[index]Get/Set Bit - Bitwise Operations: And, Or, Xor, Not (Mutate In-Place)
- CopyTo() คัดลอกไป Array (bool[], byte[], int[])
- ประหยัด Memory มากกว่า bool[] 8 เท่า