Export (0) Print
Expand All

BitArray.And Method

Performs the bitwise AND operation on the elements in the current BitArray against the corresponding elements in the specified BitArray.

[Visual Basic]
Public Function And( _
   ByVal value As BitArray _
) As BitArray
[C#]
public BitArray And(
 BitArray value
);
[C++]
public: BitArray* And(
 BitArray* value
);
[JScript]
public function And(
   value : BitArray
) : BitArray;

Parameters

value
The BitArray with which to perform the bitwise AND operation.

Return Value

A BitArray containing the result of the bitwise AND operation on the elements in the current BitArray against the corresponding elements in the specified BitArray.

Exceptions

Exception Type Condition
ArgumentNullException value is a null reference (Nothing in Visual Basic).
ArgumentException value and the current BitArray do not have the same number of elements.

Remarks

The bitwise AND operation returns true if both operands are true, and returns false if one or both operands are false.

Example

[Visual Basic, C#, C++] The following example shows how to apply AND to two BitArray instances.

[Visual Basic] 
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesBitArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes two BitArrays of the same size.
        Dim myBA1 As New BitArray(4)
        Dim myBA2 As New BitArray(4)
        myBA1(1) = False
        myBA1(0) = False
        myBA1(3) = True
        myBA1(2) = True
        myBA2(2) = False
        myBA2(0) = False
        myBA2(3) = True
        myBA2(1) = True
        
        ' Performs a bitwise AND operation between BitArray instances of the same size.
        Console.WriteLine("Initial values")
        Console.Write("myBA1:")
        PrintValues(myBA1, 8)
        Console.Write("myBA2:")
        PrintValues(myBA2, 8)
        Console.WriteLine()
        
        Console.WriteLine("Result")
        Console.Write("AND:")
        PrintValues(myBA1.And(myBA2), 8)
        Console.WriteLine()
        
        Console.WriteLine("After AND")
        Console.Write("myBA1:")
        PrintValues(myBA1, 8)
        Console.Write("myBA2:")
        PrintValues(myBA2, 8)
        Console.WriteLine()
        
        ' Performing AND between BitArray instances of different sizes returns an exception.
        Try
            Dim myBA3 As New BitArray(8)
            myBA3(0) = False
            myBA3(1) = False
            myBA3(2) = False
            myBA3(3) = False
            myBA3(4) = True
            myBA3(5) = True
            myBA3(6) = True
            myBA3(7) = True
            myBA1.And(myBA3)
        Catch myException As Exception
            Console.WriteLine("Exception: " + myException.ToString())
        End Try
    End Sub
    
    Public Shared Sub PrintValues(myList As IEnumerable, myWidth As Integer)
        Dim myEnumerator As System.Collections.IEnumerator = _
           myList.GetEnumerator()
        Dim i As Integer = myWidth
        While myEnumerator.MoveNext()
            If i <= 0 Then
                i = myWidth
                Console.WriteLine()
            End If
            i -= 1
            Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' Initial values
' myBA1:    False    False    True    True
' myBA2:    False    True    False    True
' 
' Result
' AND:    False    False    False    True
' 
' After AND
' myBA1:    False    False    False    True
' myBA2:    False    True    False    True
' 
' Exception: System.ArgumentException: Array lengths must be the same.
'    at System.Collections.BitArray.And(BitArray value) in <UnknownName>:line 
'    at SamplesBitArray.Main() 

[C#] 
using System;
using System.Collections;
public class SamplesBitArray  {

   public static void Main()  {

      // Creates and initializes two BitArrays of the same size.
      BitArray myBA1 = new BitArray( 4 );
      BitArray myBA2 = new BitArray( 4 );
      myBA1[0] = myBA1[1] = false;
      myBA1[2] = myBA1[3] = true;
      myBA2[0] = myBA2[2] = false;
      myBA2[1] = myBA2[3] = true;

      // Performs a bitwise AND operation between BitArray instances of the same size.
      Console.WriteLine( "Initial values" );
      Console.Write( "myBA1:" );
      PrintValues( myBA1, 8 );
      Console.Write( "myBA2:" );
      PrintValues( myBA2, 8 );
      Console.WriteLine();

      Console.WriteLine( "Result" );
      Console.Write( "AND:" );
      PrintValues( myBA1.And( myBA2 ), 8 );
      Console.WriteLine();

      Console.WriteLine( "After AND" );
      Console.Write( "myBA1:" );
      PrintValues( myBA1, 8 );
      Console.Write( "myBA2:" );
      PrintValues( myBA2, 8 );
      Console.WriteLine();

      // Performing AND between BitArray instances of different sizes returns an exception.
      try  {
         BitArray myBA3 = new BitArray( 8 );
         myBA3[0] = myBA3[1] = myBA3[2] = myBA3[3] = false;
         myBA3[4] = myBA3[5] = myBA3[6] = myBA3[7] = true;
         myBA1.And( myBA3 );
      } catch ( Exception myException )  {
         Console.WriteLine("Exception: " + myException.ToString());
      }
   }


   public static void PrintValues( IEnumerable myList, int myWidth )  {
      System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
      int i = myWidth;
      while ( myEnumerator.MoveNext() )  {
         if ( i <= 0 )  {
            i = myWidth;
            Console.WriteLine();
         }
         i--;
         Console.Write( "\t{0}", myEnumerator.Current );
      }
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

Initial values
myBA1:    False    False    True    True
myBA2:    False    True    False    True

Result
AND:    False    False    False    True

After AND
myBA1:    False    False    False    True
myBA2:    False    True    False    True

Exception: System.ArgumentException: Array lengths must be the same.
   at System.Collections.BitArray.And(BitArray value) in <UnknownName>:line 0
   at SamplesBitArray.Main()
*/ 

[C++] 
#using <mscorlib.dll>
#using <system.dll>

using namespace System;
using namespace System::Collections;


void PrintValues( IEnumerable* myList, int myWidth )  {
   System::Collections::IEnumerator* myEnumerator = myList->GetEnumerator();
   int i = myWidth;
   while ( myEnumerator->MoveNext() )  {
      if ( i <= 0 )  {
         i = myWidth;
         Console::WriteLine();
      }
      i--;
      Console::Write( S"\t{0}", myEnumerator->Current );
   }
   Console::WriteLine();
}


int main()  {

   // Creates and initializes two BitArrays of the same size.
   const int ArraySize = 4;

   BitArray* myBA1 = new BitArray( ArraySize );
   BitArray* myBA2 = new BitArray( ArraySize );

   for( int i(0); i < ArraySize; ++i ) {
      myBA1->Item[i] = (i % 2 == 0);
      myBA2->Item[i] = !myBA1->Item[i];
   }

   // Performs a bitwise AND operation between BitArray instances of the same size.
   Console::WriteLine( S"Initial values" );
   Console::Write( S"myBA1:" );
   PrintValues( myBA1, 8 );
   Console::Write( S"myBA2:" );
   PrintValues( myBA2, 8 );
   Console::WriteLine();

   Console::WriteLine( S"Result" );
   Console::Write( S"AND:" );
   PrintValues( myBA1->And( myBA2 ), 8 );
   Console::WriteLine();

   Console::WriteLine( S"After AND" );
   Console::Write( S"myBA1:" );
   PrintValues( myBA1, 8 );
   Console::Write( S"myBA2:" );
   PrintValues( myBA2, 8 );
   Console::WriteLine();

   // Performing AND between BitArray instances of different sizes returns an exception.
   try  {
      BitArray* myBA3 = new BitArray( 8 );

      for( int i(0); i < 2*ArraySize; ++i ) {
         myBA3->Item[i] = (i % 4 != 0);
      }

      myBA1->And( myBA3 );
   } catch ( Exception* myException )  {
      Console::WriteLine( String::Concat(S"Exception: ",myException->ToString()) );
   }
}

/*
This code produces the following output.

Initial values
myBA1:  True    False   True    False
myBA2:  False   True    False   True

Result
AND:    False   False   False   False

After AND
myBA1:  False   False   False   False
myBA2:  False   True    False   True

Exception: System.ArgumentException: Array lengths must be the same.
at System.Collections.BitArray.And(BitArray value) in <UnknownName>:line 0
at SamplesBitArray.Main()
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

BitArray Class | BitArray Members | System.Collections Namespace

Show:
© 2015 Microsoft