C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Next: We use the BitArray type. This example initializes a BitArray from a bool array.
Here: The program creates a bool array with true and false values, and then the BitArray constructor converts those into one bit each.
Bool ArrayInfo: This means that instead of one byte for a bool, the values are stored as one bit, in one-eighth the space.
True, FalseC# program that creates new BitArray
using System;
using System.Collections;
class Program
{
static void Main()
{
// Create array of 5 elements and 3 true values.
bool[] array = new bool[5];
array[0] = true;
array[1] = false; // <-- False value is default
array[2] = true;
array[3] = false;
array[4] = true;
// Create BitArray from the array.
BitArray bitArray = new BitArray(array);
// Display all bits.
foreach (bool bit in bitArray)
{
Console.WriteLine(bit);
}
}
}
Output
True
False
True
False
True
Count: The Count on BitArray instances does not return the number of set bits. Instead, it returns the count of all bits of any value.
Therefore: You will need to loop over bits individually to count them. You can also use a for-loop.
C# program that sets and counts bits
using System;
using System.Collections;
class Program
{
static void Main()
{
// Create BitArray from the array.
BitArray bitArray = new BitArray(32);
// Set three bits to 1.
bitArray[3] = true; // You can set the bits with the indexer.
bitArray[5] = true;
bitArray.Set(10, true); // You can set the bits with Set.
// Count returns the total of all bits (1s and 0s).
Console.WriteLine("--- Total bits ---");
Console.WriteLine(bitArray.Count);
// You can loop to count set bits.
Console.WriteLine("--- Total bits set to 1 ---");
Console.WriteLine(CountBitArray(bitArray));
}
/// <summary>
/// Count set bits in BitArray.
/// </summary>
static int CountBitArray(BitArray bitArray)
{
int count = 0;
foreach (bool bit in bitArray)
{
if (bit)
{
count++;
}
}
return count;
}
}
Output
--- Total bits ---
32
--- Total bits set to 1 ---
3
C# program that displays bits and uses And
using System;
using System.Collections;
class Program
{
static void Main()
{
//
// Initialize BitArray with 4 true bits and 12 false bits.
//
BitArray bitArray1 = new BitArray(16);
bitArray1.Set(0, true);
bitArray1.Set(1, true);
bitArray1.Set(4, true);
bitArray1.Set(5, true);
//
// Display the BitArray.
//
DisplayBitArray(bitArray1);
//
// Initialize BitArray with two set bits.
//
BitArray bitArray2 = new BitArray(16);
bitArray2.Set(0, true);
bitArray2.Set(7, true);
DisplayBitArray(bitArray2);
//
// And the bits.
//
bitArray1.And(bitArray2);
DisplayBitArray(bitArray1);
}
/// <summary>
/// Display bits as 0s and 1s.
/// </summary>
static void DisplayBitArray(BitArray bitArray)
{
for (int i = 0; i < bitArray.Count; i++)
{
bool bit = bitArray.Get(i);
Console.Write(bit ? 1 : 0);
}
Console.WriteLine();
}
}
Output
1100110000000000
1000000100000000
1000000000000000
Note: You can find more reference on the behavior of these operators on Wikipedia.
Internal members of BitArray:
private int[] m_array;
private int m_length;
Therefore: The BitArray is unsuitable for performance-sensitive applications that access many separate bits.