BitArray Class
BitArray จัดการ Compact Array ของค่า Bit (Boolean) - true = 1 (On), false = 0 (Off) ใช้ namespace System.Collections
คุณสมบัติ (Properties)
| Property |
คำอธิบาย |
Count |
จำนวน Bit ทั้งหมด |
IsReadOnly |
เป็น Read-Only หรือไม่ |
Item[int index] |
Get/Set ค่า Bit ที่ Index (Indexer) |
Length |
จำนวน Bit (Get/Set - เปลี่ยนขนาดได้) |
เมธอด Index-based I/O (Bitwise Operations)
| Method |
คำอธิบาย |
And(BitArray value) |
Bitwise AND กับ BitArray อื่น |
Get(int index) |
ดึงค่า Bit ที่ Index (เหมือน Item[index]) |
Not() |
Invert ทุก Bit (0→1, 1→0) |
Or(BitArray value) |
Bitwise OR กับ BitArray อื่น |
Set(int index, bool value) |
ตั้งค่า Bit ที่ Index |
SetAll(bool value) |
ตั้งค่า Bit ทั้งหมด |
Xor(BitArray value) |
Bitwise XOR กับ BitArray อื่น |
ตัวอย่างการใช้งาน
using System;
using System.Collections;
public class Demo {
public static void Main() {
// สร้าง BitArray 10 bits (ทั้งหมด false = 0)
BitArray bits = new BitArray(10);
Console.WriteLine("Count: " + bits.Count); // 10
Console.WriteLine("Length: " + bits.Length); // 10
// ตั้งค่า Bit บางตัว
bits.Set(0, true); // Bit 0 = 1
bits.Set(3, true); // Bit 3 = 1
bits[5] = true; // ใช้ Indexer
Console.WriteLine("\nBits after setting:");
for (int i = 0; i < bits.Count; i++) {
Console.Write(bits[i] ? "1" : "0");
}
// Output: 1001010000
// Bitwise Operations
BitArray mask = new BitArray(new bool[] { true, false, true, false, true, false, true, false, true, false });
BitArray andResult = bits.And(mask);
BitArray orResult = bits.Or(mask);
BitArray xorResult = bits.Xor(mask);
BitArray notResult = bits.Not();
Console.WriteLine("\n\nAND with mask: " + BitArrayToString(andResult));
Console.WriteLine("OR with mask: " + BitArrayToString(orResult));
Console.WriteLine("XOR with mask: " + BitArrayToString(xorResult));
Console.WriteLine("NOT: " + BitArrayToString(notResult));
}
static string BitArrayToString(BitArray ba) {
char[] chars = new char[ba.Count];
for (int i = 0; i < ba.Count; i++) {
chars[i] = ba[i] ? '1' : '0';
}
return new string(chars);
}
}
การสร้าง BitArray จากแหล่งข้อมูลต่างๆ
// 1. จากขนาด (ทั้งหมด false)
BitArray ba1 = new BitArray(16);
// 2. จาก bool[]
bool[] boolArray = { true, false, true, true };
BitArray ba2 = new BitArray(boolArray);
// 3. จาก byte[] (แต่ละ byte = 8 bits)
byte[] bytes = { 0xFF, 0x00 }; // 11111111 00000000
BitArray ba3 = new BitArray(bytes);
// 4. จาก int[] (แต่ละ int = 32 bits)
int[] ints = { 0xFFFFFFFF, 0 };
BitArray ba4 = new BitArray(ints);
// 5. Clone
BitArray ba5 = (BitArray)ba1.Clone();
การประยุกต์ใช้งานจริง
- Flags/Permissions: จัดการ Permission หลายตัวใน Integer เดียว
- Bloom Filters: Probabilistic Data Structure
- Bitset Operations: Union, Intersection, Difference ของ Sets
- Compression: บีบอัด Boolean Array ให้เล็กลง 8 เท่า
- Graphics: Bitmap, Mask, Stencil Buffer
ตัวอย่าง: Permission Flags
[Flags]
enum Permissions : byte {
None = 0,
Read = 1 << 0, // 1
Write = 1 << 1, // 2
Execute = 1 << 2, // 4
Delete = 1 << 3, // 8
Admin = Read | Write | Execute | Delete // 15
}
// ใช้ BitArray จัดการ
BitArray userPerms = new BitArray(new byte[] { (byte)Permissions.Read | (byte)Permissions.Write });
// userPerms: 00000011 (Read + Write)
bool canRead = userPerms[0]; // true
bool canDelete = userPerms[3]; // false