Title: Use the BitArray class in C#
The BitArray class stores an array of Boolean values packed into a bit array to save space. The size of the BitArray in bytes depends on the number of bits it contains and on the size of the word on the computer. For example, a BitArray containing a single bit will probably occupy 32 bits.
The following list describes the class's most useful methods.
- And - Uses logical And to combine the values in a BitArray with the corresponding values in another BitArray.
- Not - Inverts the values in the BitArray.
- Or - Uses logical Or to combine the values in a BitArray with the corresponding values in another BitArray.
- SetAll - Sets all of the values in the array to true or false.
- Xor - Uses logical Xor to combine the values in a BitArray with the corresponding values in another BitArray.
This example uses the following code to demonstrate some of the BitArray class's features.
// Demonstrate the BitArray class.
private void Form1_Load(object sender, EventArgs e)
{
string txt = "";
// Set all bits to true then false.
BitArray bits = new BitArray(32);
bits.SetAll(true);
txt += "True: " + BitArrayToString(bits) + "\r\n";
bits.SetAll(false);
txt += "False: " + BitArrayToString(bits) + "\r\n";
// Set bits[i] true if i is a Fibonacci number.
BitArray is_fibonacci = new BitArray(bits.Length);
for (int i = 0; i < bits.Length; i++)
is_fibonacci[i] = IsFibonacci(i);
txt += "Fibonacci: " +
BitArrayToString(is_fibonacci) + "\r\n";
// Invert the bits.
is_fibonacci.Not();
for (int i = 0; i < bits.Length; i++)
bits[i] = IsFibonacci(i);
txt += "~Fibonacci: " +
BitArrayToString(is_fibonacci) + "\r\n";
// Set a bit true if i is a perfect square.
BitArray is_square = new BitArray(bits.Length);
for (int i = 0; i < bits.Length; i++)
is_square[i] = IsSquare(i);
txt += "Square: " +
BitArrayToString(is_square) + "\r\n";
// And with is_square.
BitArray sqaure_and_not_fibonacci =
is_square.And(is_fibonacci);
is_fibonacci.Not();
for (int i = 0; i < bits.Length; i++)
bits[i] = IsFibonacci(i);
txt += "Sq && ~Fib: " +
BitArrayToString(sqaure_and_not_fibonacci);
// Display the results.
txtBits.Text = txt;
txtBits.Select(0, 0);
}
The code creates a BitArray, calls its SetAll method to set all of its values to true, and then calls the BitArrayToString method shown shortly to add the values in the array to a string. It then uses SetAll to set the array's values to false and adds their values to the string.
Next the code creates a BitArray named is_fibonacci, sets is_fibonacci[i] to true if i is a Fibonacci number, and adds those values to the result string.
The program then uses the BitArray class's Not method to invert the array and adds those values to the output string.
The code follows steps similar to those it used to deal with Fibonacci numbers to display values indicating perfect squares.
Finally the code uses And to combine the negated Fibonacci values and the perfect square values, adds the result to the output string, and displays the result in a TextBox.
The following code shows helper methods that the program uses to determine whether a number is a perfect square or Fibonacci number, and to display a BitArray object's values as a string.
// Return true if i is a perfect square.
private bool IsSquare(int i)
{
int sqrt_i = (int)Math.Sqrt(i);
return (sqrt_i * sqrt_i == i);
}
// Return true if i is a Fibonacci number.
private bool IsFibonacci(int i)
{
if (i == 0) return true;
long fib_i_minus_1 = 0;
long fib_i = 1;
while (fib_i < i)
{
long fib_i_plus_1 = fib_i + fib_i_minus_1;
fib_i_minus_1 = fib_i;
fib_i = fib_i_plus_1;
}
return (i == fib_i);
}
// Return a string showing the BitArray's values.
private string BitArrayToString(BitArray bits)
{
string result = "";
foreach (bool value in bits)
{
if (value) result += "1";
else result += "0";
}
return result;
}
Download the example to experiment with it and to see additional details.
|