This documentation is archived and is not being maintained.

BitArray.Xor Method

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

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

Parameters

value
The BitArray with which to perform the bitwise eXclusive OR operation.

Return Value

A BitArray containing the result of the bitwise eXclusive OR 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 exclusive OR operation returns true if exactly one operand is true, and returns false if both operands have the same Boolean value.

Example

[Visual Basic, C#, C++] The following example shows how to apply XOR 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(0) = False
        myBA1(1) = False
        myBA1(2) = True
        myBA1(3) = True
        myBA2(0) = False
        myBA2(2) = False
        myBA2(1) = True
        myBA2(3) = True
        
        ' Performs a bitwise XOR 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("XOR:")
        PrintValues(myBA1.Xor(myBA2), 8)
        Console.WriteLine()
        
        Console.WriteLine("After XOR")
        Console.Write("myBA1:")
        PrintValues(myBA1, 8)
        Console.Write("myBA2:")
        PrintValues(myBA2, 8)
        Console.WriteLine()
        
        ' Performing XOR 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.Xor(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
' XOR:    False    True    True    False
' 
' After XOR
' myBA1:    False    True    True    False
' myBA2:    False    True    False    True
' 
' Exception: System.ArgumentException: Array lengths must be the same.
'    at System.Collections.BitArray.Xor(BitArray value) in <UnknownName>:line 0
'    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 XOR 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( "XOR:" );
      PrintValues( myBA1.Xor( myBA2 ), 8 );
      Console.WriteLine();

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

      // Performing XOR 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.Xor( 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
XOR:    False    True    True    False

After XOR
myBA1:    False    True    True    False
myBA2:    False    True    False    True

Exception: System.ArgumentException: Array lengths must be the same.
   at System.Collections.BitArray.Xor(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 XOR 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"XOR:" );
   PrintValues( myBA1->Xor( myBA2 ), 8 );
   Console::WriteLine();

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

   // Performing OR between BitArray instances of different sizes returns an exception.
   try  {
      BitArray* myBA3 = new BitArray( 2*ArraySize );
      for( int i(0); i < 2*ArraySize; ++i ) {
         myBA3->Item[i] = (i % 4 != 0);
      }

      myBA1->Xor( 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
XOR:    True    True    True    True

After XOR
myBA1:  True    True    True    True
myBA2:  False   True    False   True

Exception: System.ArgumentException: Array lengths must be the same.
at System.Collections.BitArray.Or(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: