Export (0) Print
Expand All

BitArray.Not Method

Inverts all the bit values in the current BitArray, so that elements set to true are changed to false, and elements set to false are changed to true.

[Visual Basic]
Public Function Not() As BitArray
[C#]
public BitArray Not();
[C++]
public: BitArray* Not();
[JScript]
public function Not() : BitArray;

Return Value

The current instance with inverted bit values.

Example

[Visual Basic, C#, C++] The following example shows how to apply NOT to a BitArray.

[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 NOT 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()
        
        myBA1.Not()
        myBA2.Not()
        
        Console.WriteLine("After NOT")
        Console.Write("myBA1:")
        PrintValues(myBA1, 8)
        Console.Write("myBA2:")
        PrintValues(myBA2, 8)
        Console.WriteLine()
    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
' 
' After NOT
' myBA1:    True    True    False    False
' myBA2:    True    False    True    False 

[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 NOT 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();

      myBA1.Not();
      myBA2.Not();

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


   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

After NOT
myBA1:    True    True    False    False
myBA2:    True    False    True    False
*/ 

[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] = !myBA2->Item[i];
   }

   // Performs a bitwise NOT 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();

   myBA1->Not();
   myBA2->Not();

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

/*
This code produces the following output.

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

After NOT
myBA1:  False   True    False   True
myBA2:  False   False   False   False
*/

[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:
© 2014 Microsoft