Mono Class Library: System.Collections.BitArray Overview | Members

System.Collections.BitArray.And Method

Performs a bitwise And on two BitArray instances.

public BitArray And (BitArray value)

Parameters

value
The BitArray to And with the current instance.

Returns

A BitAray whose bit values are the bitwise And of the BItArray instance and the argument BitArray.

Remarks

The result of the And operation overwrites the original instance.

The two BitArrays must be the same length. If they are not an ArgumentException will be thrown.

C# Example

using System;
using System.Collections;
using System.Text;

        public class BitArrayAnd
        {
                public static void Main ()
                {
                        BitArray leftOperand = new BitArray (new byte [] {byte.Parse ("9")});
                        BitArray rightOperand = new BitArray (new byte [] {byte.Parse ("10")});
                        BitArrayOut.PrintBitArray (leftOperand, " And ");
                        BitArrayOut.PrintBitArray (rightOperand, " = ");
                        leftOperand.And (rightOperand);
                        BitArrayOut.PrintBitArray (leftOperand, "\n");
                }
        }

        public static class BitArrayOut
        {
                internal static void PrintBitArray (BitArray bitArray)
                {
                        Console.Write (BitArrayToString (bitArray));
                }

                internal static void PrintBitArray (BitArray bitArray, string symbol)
                {
                        PrintBitArray (bitArray);
                        Console.Write (symbol);
                }

                private static string BitArrayToString (BitArray bitArray)
                {
                        StringBuilder stringBuilder = new StringBuilder ();
                        for (int i = bitArray.Count - 1; i > -1; i--) {
                                char digit = bitArray[i] ? '1' : '0';
                                stringBuilder.Append (digit);
                        }

                        return stringBuilder.ToString();
                }
        }
  

Compiling and running the above example generates the following output:

00001001 And 00001010 = 00001000

Requirements

Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0