Visual C++ .NET/Collections/BitArray — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 12:05, 26 мая 2010

And operation for two BitArray

 
#include "stdafx.h"
using namespace System;
using namespace System::Collections;
void Print( BitArray ^barray, String ^desc)
{
    Console::WriteLine(desc);
    int i = 0;
    for each( bool^ val in barray )
    {
        Console::Write("{0} ", val);
        if (++i > 7)
        {
            Console::WriteLine();
            i = 0;
        }
    }
    Console::WriteLine();
}
void main()
{
    BitArray ^barray1 = gcnew BitArray( 8, true );
    Print(barray1, "BitArray( 8, true );");
    BitArray ^barray2 = gcnew BitArray( 8, true );
    barray2->And(barray1);
    Print(barray2, "And with BitArray( 8, true )");
}


Creating BitArray

 
#include "stdafx.h"
using namespace System;
using namespace System::Collections;
void Print( BitArray ^barray, String ^desc)
{
    Console::WriteLine(desc);
    int i = 0;
    for each( bool^ val in barray )
    {
        Console::Write("{0} ", val);
        if (++i > 7)
        {
            Console::WriteLine();
            i = 0;
        }
    }
    Console::WriteLine();
}
void main()
{
    BitArray ^barray1 = gcnew BitArray( 8, true );
    Print(barray1, "BitArray( 8, true );");

}


Creating BitArray from array

 
#include "stdafx.h"
using namespace System;
using namespace System::Collections;
void Print( BitArray ^barray, String ^desc)
{
    Console::WriteLine(desc);
    int i = 0;
    for each( bool^ val in barray )
    {
        Console::Write("{0} ", val);
        if (++i > 7)
        {
            Console::WriteLine();
            i = 0;
        }
    }
    Console::WriteLine();
}
void main()
{
    array<unsigned char>^ chars = gcnew array<unsigned char> { 0x55, 0xAA };
    BitArray ^barray3 = gcnew BitArray( chars );
    Print(barray3, "BitArray(0x55, 0xAA);");
    Console::WriteLine("Item[0]={0}", barray3[0]);
    Console::WriteLine("Item[8]={0}", barray3[8]);
}


Not on value in BitArray

 
#include "stdafx.h"
using namespace System;
using namespace System::Collections;
void Print( BitArray ^barray, String ^desc)
{
    Console::WriteLine(desc);
    int i = 0;
    for each( bool^ val in barray )
    {
        Console::Write("{0} ", val);
        if (++i > 7)
        {
            Console::WriteLine();
            i = 0;
        }
    }
    Console::WriteLine();
}
void main()
{
    BitArray ^barray1 = gcnew BitArray( 8, true );
    Print(barray1, "BitArray( 8, true );");
    barray1[1] = false;
    barray1->Not();
    Print(barray1, "Modified bit 1&4 then Not");
}


Or operation between two BitArray

 
#include "stdafx.h"
using namespace System;
using namespace System::Collections;
void Print( BitArray ^barray, String ^desc)
{
    Console::WriteLine(desc);
    int i = 0;
    for each( bool^ val in barray )
    {
        Console::Write("{0} ", val);
        if (++i > 7)
        {
            Console::WriteLine();
            i = 0;
        }
    }
    Console::WriteLine();
}
void main()
{
    BitArray ^barray1 = gcnew BitArray( 8, true );
    Print(barray1, "BitArray( 8, true );");
    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 )");
}


Set all value in BitArray to true

 
#include "stdafx.h"
using namespace System;
using namespace System::Collections;
void Print( BitArray ^barray, String ^desc)
{
    Console::WriteLine(desc);
    int i = 0;
    for each( bool^ val in barray )
    {
        Console::Write("{0} ", val);
        if (++i > 7)
        {
            Console::WriteLine();
            i = 0;
        }
    }
    Console::WriteLine();
}
void main()
{
    BitArray ^barray1 = gcnew BitArray( 8, true );
    Print(barray1, "BitArray( 8, true );");
    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 )");
}


Set value in BitArray by indexer

 
#include "stdafx.h"
using namespace System;
using namespace System::Collections;
void Print( BitArray ^barray, String ^desc)
{
    Console::WriteLine(desc);
    int i = 0;
    for each( bool^ val in barray )
    {
        Console::Write("{0} ", val);
        if (++i > 7)
        {
            Console::WriteLine();
            i = 0;
        }
    }
    Console::WriteLine();
}
void main()
{
    BitArray ^barray1 = gcnew BitArray( 8, true );
    Print(barray1, "BitArray( 8, true );");
    barray1[1] = false;
    barray1->Not();
    Print(barray1, "Modified bit 1&4 then Not");
}


Xor operation between two BitArray

 
#include "stdafx.h"
using namespace System;
using namespace System::Collections;
void Print( BitArray ^barray, String ^desc)
{
    Console::WriteLine(desc);
    int i = 0;
    for each( bool^ val in barray )
    {
        Console::Write("{0} ", val);
        if (++i > 7)
        {
            Console::WriteLine();
            i = 0;
        }
    }
    Console::WriteLine();
}
void main()
{
    BitArray ^barray1 = gcnew BitArray( 8, true );
    Print(barray1, "BitArray( 8, true );");
    BitArray ^barray2 = gcnew BitArray( 8, true );
    barray2->And(barray1);
    Print(barray2, "And with BitArray( 8, true )");
    
    barray2->SetAll(true);
    barray2->Xor(barray1);
    Print(barray2, "XOr with BitArray( 8, true )");
}