Visual C++ et Collection: Exemple d’utilisation de BitArray

Author:


{filelink=13221}

#include "stdafx.h"

using namespace System;
using namespace System::Collections;


 void Print( BitArray ^byteArray, String ^desc)
{
   for ( int i = 0; i Count; i++ ) {
      //char dc =  *(Byte^)   byteArray->default[i];
	   Console::Write("\n{0}: {1}", desc, byteArray->default[i]);
   }
   
   Console::WriteLine();
	
}

Int32 main(void)
{
	try
	{
    BitArray ^barray1 = gcnew BitArray( 8, true );
    Print(barray1, "BitArray( 8, true );");

    barray1->default[1] = false;
    barray1->default[4] = false;
    barray1->Not();
    Print(barray1, "Modified bit 1&4 then Not");

    BitArray ^barray2 = gcnew BitArray( 8, true );
    barray2->And(barray1);
    Print(barray2, "And with BitArray( 8, true )");
    barray2->SetAll(true);
    barray2->Or(barray1);
    Print(barray2, "Or with BitArray( 8, true )");

    barray2->SetAll(true);
    barray2->Xor(barray1);
    Print(barray2, "Xor with BitArray( 8, true )");

    Console::WriteLine("");

    Byte bytes[] = { 0x55,  0xAA };
    BitArray ^barray3 = gcnew BitArray( (int)bytes );
    Print(barray3, "BitArray(0x55,  0xAA);");

    Console::WriteLine("Item[0]={0}", barray3->default[0]); 
    Console::WriteLine("Item[8]={0}", barray3->default[8]);
	}  catch (Exception ^e)
    {
        Console::WriteLine("Invalid Cast Exception");
        Console::WriteLine(e->StackTrace);
    }
    Console::ReadLine();
    return 0;
}




Leave a Reply

Your email address will not be published. Required fields are marked *