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 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'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) + Environment.NewLine;
    bits.SetAll(false);
    txt += "False:      " + BitArrayToString(bits) + Environment.NewLine;

    // 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) + Environment.NewLine;

    // Invert the bits.
    is_fibonacci.Not();
    for (int i = 0; i < bits.Length; i++) bits[i] = IsFibonacci(i);
    txt += "~Fibonacci: " + BitArrayToString(is_fibonacci) + Environment.NewLine;

    // 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) + Environment.NewLine;

    // 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 helped methods that the program uses to determine whether a number is a perfect square or Fibonacci number, and to display a BitArray'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 += "T";
        else result += "F";
    }
    return result;
}

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.