BitVector32 Structure

Définition

Fournit une structure simple qui stocke des valeurs booléennes et des petits entiers dans 32 bits de mémoire.

public value class BitVector32
public value class BitVector32 : IEquatable<System::Collections::Specialized::BitVector32>
public struct BitVector32
public struct BitVector32 : IEquatable<System.Collections.Specialized.BitVector32>
type BitVector32 = struct
Public Structure BitVector32
Public Structure BitVector32
Implements IEquatable(Of BitVector32)
Héritage
BitVector32
Implémente

Exemples

L’exemple de code suivant utilise un BitVector32 comme collection d’indicateurs de bits.

#using <system.dll>

using namespace System;
using namespace System::Collections::Specialized;
int main()
{
   
   // Creates and initializes a BitVector32 with all bit flags set to FALSE.
   BitVector32 myBV(0);
   
   // Creates masks to isolate each of the first five bit flags.
   int myBit1 = BitVector32::CreateMask();
   int myBit2 = BitVector32::CreateMask( myBit1 );
   int myBit3 = BitVector32::CreateMask( myBit2 );
   int myBit4 = BitVector32::CreateMask( myBit3 );
   int myBit5 = BitVector32::CreateMask( myBit4 );
   
   // Sets the alternating bits to TRUE.
   Console::WriteLine( "Setting alternating bits to TRUE:" );
   Console::WriteLine( "   Initial:       {0}", myBV );
   myBV[ myBit1 ] = true;
   Console::WriteLine( "   myBit1 = TRUE: {0}", myBV );
   myBV[ myBit3 ] = true;
   Console::WriteLine( "   myBit3 = TRUE: {0}", myBV );
   myBV[ myBit5 ] = true;
   Console::WriteLine( "   myBit5 = TRUE: {0}", myBV );
}

/*
This code produces the following output.

Setting alternating bits to TRUE:
Initial:         BitVector32 {00000000000000000000000000000000}
myBit1 = TRUE:   BitVector32 {00000000000000000000000000000001}
myBit3 = TRUE:   BitVector32 {00000000000000000000000000000101}
myBit5 = TRUE:   BitVector32 {00000000000000000000000000010101}


*/
using System;
using System.Collections.Specialized;

public class SamplesBitVector32  {

   public static void Main()  {

      // Creates and initializes a BitVector32 with all bit flags set to FALSE.
      BitVector32 myBV = new BitVector32( 0 );

      // Creates masks to isolate each of the first five bit flags.
      int myBit1 = BitVector32.CreateMask();
      int myBit2 = BitVector32.CreateMask( myBit1 );
      int myBit3 = BitVector32.CreateMask( myBit2 );
      int myBit4 = BitVector32.CreateMask( myBit3 );
      int myBit5 = BitVector32.CreateMask( myBit4 );

      // Sets the alternating bits to TRUE.
      Console.WriteLine( "Setting alternating bits to TRUE:" );
      Console.WriteLine( "   Initial:         {0}", myBV.ToString() );
      myBV[myBit1] = true;
      Console.WriteLine( "   myBit1 = TRUE:   {0}", myBV.ToString() );
      myBV[myBit3] = true;
      Console.WriteLine( "   myBit3 = TRUE:   {0}", myBV.ToString() );
      myBV[myBit5] = true;
      Console.WriteLine( "   myBit5 = TRUE:   {0}", myBV.ToString() );
   }
}

/*
This code produces the following output.

Setting alternating bits to TRUE:
   Initial:         BitVector32{00000000000000000000000000000000}
   myBit1 = TRUE:   BitVector32{00000000000000000000000000000001}
   myBit3 = TRUE:   BitVector32{00000000000000000000000000000101}
   myBit5 = TRUE:   BitVector32{00000000000000000000000000010101}


*/
Imports System.Collections.Specialized

Public Class SamplesBitVector32

   Public Shared Sub Main()

      ' Creates and initializes a BitVector32 with all bit flags set to FALSE.
      Dim myBV As New BitVector32(0)

      ' Creates masks to isolate each of the first five bit flags.
      Dim myBit1 As Integer = BitVector32.CreateMask()
      Dim myBit2 As Integer = BitVector32.CreateMask(myBit1)
      Dim myBit3 As Integer = BitVector32.CreateMask(myBit2)
      Dim myBit4 As Integer = BitVector32.CreateMask(myBit3)
      Dim myBit5 As Integer = BitVector32.CreateMask(myBit4)

      ' Sets the alternating bits to TRUE.
      Console.WriteLine("Setting alternating bits to TRUE:")
      Console.WriteLine("   Initial:         {0}", myBV.ToString())
      myBV(myBit1) = True
      Console.WriteLine("   myBit1 = TRUE:   {0}", myBV.ToString())
      myBV(myBit3) = True
      Console.WriteLine("   myBit3 = TRUE:   {0}", myBV.ToString())
      myBV(myBit5) = True
      Console.WriteLine("   myBit5 = TRUE:   {0}", myBV.ToString())
   End Sub
End Class


' This code produces the following output.
'
' Setting alternating bits to TRUE:
'    Initial:         BitVector32{00000000000000000000000000000000}
'    myBit1 = TRUE:   BitVector32{00000000000000000000000000000001}
'    myBit3 = TRUE:   BitVector32{00000000000000000000000000000101}
'    myBit5 = TRUE:   BitVector32{00000000000000000000000000010101}

L’exemple de code suivant utilise un BitVector32 comme collection de sections.

#using <system.dll>

using namespace System;
using namespace System::Collections::Specialized;

int main()
{
   // Creates and initializes a BitVector32.
   BitVector32 myBV(0);

   // Creates four sections in the BitVector32 with maximum values 6, 3, 1, and 15.
   // mySect3, which uses exactly one bit, can also be used as a bit flag.
   BitVector32::Section mySect1 = BitVector32::CreateSection( 6 );
   BitVector32::Section mySect2 = BitVector32::CreateSection( 3, mySect1 );
   BitVector32::Section mySect3 = BitVector32::CreateSection( 1, mySect2 );
   BitVector32::Section mySect4 = BitVector32::CreateSection( 15, mySect3 );

   // Displays the values of the sections.
   Console::WriteLine( "Initial values:" );
   Console::WriteLine( "\tmySect1: {0}", myBV[ mySect1 ] );
   Console::WriteLine( "\tmySect2: {0}", myBV[ mySect2 ] );
   Console::WriteLine( "\tmySect3: {0}", myBV[ mySect3 ] );
   Console::WriteLine( "\tmySect4: {0}", myBV[ mySect4 ] );

   // Sets each section to a new value and displays the value of the BitVector32 at each step.
   Console::WriteLine( "Changing the values of each section:" );
   Console::WriteLine( "\tInitial:    \t {0}", myBV );
   myBV[ mySect1 ] = 5;
   Console::WriteLine( "\tmySect1 = 5:\t {0}", myBV );
   myBV[ mySect2 ] = 3;
   Console::WriteLine( "\tmySect2 = 3:\t {0}", myBV );
   myBV[ mySect3 ] = 1;
   Console::WriteLine( "\tmySect3 = 1:\t {0}", myBV );
   myBV[ mySect4 ] = 9;
   Console::WriteLine( "\tmySect4 = 9:\t {0}", myBV );

   // Displays the values of the sections.
   Console::WriteLine( "New values:" );
   Console::WriteLine( "\tmySect1: {0}", myBV[ mySect1 ] );
   Console::WriteLine( "\tmySect2: {0}", myBV[ mySect2 ] );
   Console::WriteLine( "\tmySect3: {0}", myBV[ mySect3 ] );
   Console::WriteLine( "\tmySect4: {0}", myBV[ mySect4 ] );
}

/*
This code produces the following output.

Initial values:
        mySect1: 0
        mySect2: 0
        mySect3: 0
        mySect4: 0
Changing the values of each section:
        Initial:        BitVector32 {00000000000000000000000000000000}
        mySect1 = 5:    BitVector32 {00000000000000000000000000000101}
        mySect2 = 3:    BitVector32 {00000000000000000000000000011101}
        mySect3 = 1:    BitVector32 {00000000000000000000000000111101}
        mySect4 = 9:    BitVector32 {00000000000000000000001001111101}
New values:
        mySect1: 5
        mySect2: 3
        mySect3: 1
        mySect4: 9

*/
using System;
using System.Collections.Specialized;

public class SamplesBitVector32  {

   public static void Main()  {

      // Creates and initializes a BitVector32.
      BitVector32 myBV = new BitVector32( 0 );

      // Creates four sections in the BitVector32 with maximum values 6, 3, 1, and 15.
      // mySect3, which uses exactly one bit, can also be used as a bit flag.
      BitVector32.Section mySect1 = BitVector32.CreateSection( 6 );
      BitVector32.Section mySect2 = BitVector32.CreateSection( 3, mySect1 );
      BitVector32.Section mySect3 = BitVector32.CreateSection( 1, mySect2 );
      BitVector32.Section mySect4 = BitVector32.CreateSection( 15, mySect3 );

      // Displays the values of the sections.
      Console.WriteLine( "Initial values:" );
      Console.WriteLine( "\tmySect1: {0}", myBV[mySect1] );
      Console.WriteLine( "\tmySect2: {0}", myBV[mySect2] );
      Console.WriteLine( "\tmySect3: {0}", myBV[mySect3] );
      Console.WriteLine( "\tmySect4: {0}", myBV[mySect4] );

      // Sets each section to a new value and displays the value of the BitVector32 at each step.
      Console.WriteLine( "Changing the values of each section:" );
      Console.WriteLine( "\tInitial:    \t{0}", myBV.ToString() );
      myBV[mySect1] = 5;
      Console.WriteLine( "\tmySect1 = 5:\t{0}", myBV.ToString() );
      myBV[mySect2] = 3;
      Console.WriteLine( "\tmySect2 = 3:\t{0}", myBV.ToString() );
      myBV[mySect3] = 1;
      Console.WriteLine( "\tmySect3 = 1:\t{0}", myBV.ToString() );
      myBV[mySect4] = 9;
      Console.WriteLine( "\tmySect4 = 9:\t{0}", myBV.ToString() );

      // Displays the values of the sections.
      Console.WriteLine( "New values:" );
      Console.WriteLine( "\tmySect1: {0}", myBV[mySect1] );
      Console.WriteLine( "\tmySect2: {0}", myBV[mySect2] );
      Console.WriteLine( "\tmySect3: {0}", myBV[mySect3] );
      Console.WriteLine( "\tmySect4: {0}", myBV[mySect4] );
   }
}

/*
This code produces the following output.

Initial values:
        mySect1: 0
        mySect2: 0
        mySect3: 0
        mySect4: 0
Changing the values of each section:
        Initial:        BitVector32{00000000000000000000000000000000}
        mySect1 = 5:    BitVector32{00000000000000000000000000000101}
        mySect2 = 3:    BitVector32{00000000000000000000000000011101}
        mySect3 = 1:    BitVector32{00000000000000000000000000111101}
        mySect4 = 9:    BitVector32{00000000000000000000001001111101}
New values:
        mySect1: 5
        mySect2: 3
        mySect3: 1
        mySect4: 9

*/
Imports System.Collections.Specialized

Public Class SamplesBitVector32
   
   Public Shared Sub Main()
      
      ' Creates and initializes a BitVector32.
      Dim myBV As New BitVector32(0)
      
      ' Creates four sections in the BitVector32 with maximum values 6, 3, 1, and 15.
      ' mySect3, which uses exactly one bit, can also be used as a bit flag.
      Dim mySect1 As BitVector32.Section = BitVector32.CreateSection(6)
      Dim mySect2 As BitVector32.Section = BitVector32.CreateSection(3, mySect1)
      Dim mySect3 As BitVector32.Section = BitVector32.CreateSection(1, mySect2)
      Dim mySect4 As BitVector32.Section = BitVector32.CreateSection(15, mySect3)
      
      ' Displays the values of the sections.
      Console.WriteLine("Initial values:")
      Console.WriteLine(ControlChars.Tab + "mySect1: {0}", myBV(mySect1))
      Console.WriteLine(ControlChars.Tab + "mySect2: {0}", myBV(mySect2))
      Console.WriteLine(ControlChars.Tab + "mySect3: {0}", myBV(mySect3))
      Console.WriteLine(ControlChars.Tab + "mySect4: {0}", myBV(mySect4))
      
      ' Sets each section to a new value and displays the value of the BitVector32 at each step.
      Console.WriteLine("Changing the values of each section:")
      Console.WriteLine(ControlChars.Tab + "Initial:    " + ControlChars.Tab + "{0}", myBV.ToString())
      myBV(mySect1) = 5
      Console.WriteLine(ControlChars.Tab + "mySect1 = 5:" + ControlChars.Tab + "{0}", myBV.ToString())
      myBV(mySect2) = 3
      Console.WriteLine(ControlChars.Tab + "mySect2 = 3:" + ControlChars.Tab + "{0}", myBV.ToString())
      myBV(mySect3) = 1
      Console.WriteLine(ControlChars.Tab + "mySect3 = 1:" + ControlChars.Tab + "{0}", myBV.ToString())
      myBV(mySect4) = 9
      Console.WriteLine(ControlChars.Tab + "mySect4 = 9:" + ControlChars.Tab + "{0}", myBV.ToString())
      
      ' Displays the values of the sections.
      Console.WriteLine("New values:")
      Console.WriteLine(ControlChars.Tab + "mySect1: {0}", myBV(mySect1))
      Console.WriteLine(ControlChars.Tab + "mySect2: {0}", myBV(mySect2))
      Console.WriteLine(ControlChars.Tab + "mySect3: {0}", myBV(mySect3))
      Console.WriteLine(ControlChars.Tab + "mySect4: {0}", myBV(mySect4))

   End Sub

End Class


' This code produces the following output.
'
' Initial values:
'        mySect1: 0
'        mySect2: 0
'        mySect3: 0
'        mySect4: 0
' Changing the values of each section:
'        Initial:        BitVector32{00000000000000000000000000000000}
'        mySect1 = 5:    BitVector32{00000000000000000000000000000101}
'        mySect2 = 3:    BitVector32{00000000000000000000000000011101}
'        mySect3 = 1:    BitVector32{00000000000000000000000000111101}
'        mySect4 = 9:    BitVector32{00000000000000000000001001111101}
' New values:
'        mySect1: 5
'        mySect2: 3
'        mySect3: 1
'        mySect4: 9

Remarques

BitVector32 est plus efficace que BitArray pour les valeurs booléennes et les petits entiers utilisés en interne. Un BitArray peut croître indéfiniment en fonction des besoins, mais il a la mémoire et les performances requises par une instance de classe. En revanche, un BitVector32 utilise uniquement 32 bits.

Une BitVector32 structure peut être configurée pour contenir des sections pour les petits entiers ou des indicateurs de bits pour les booléens, mais pas les deux. Un BitVector32.Section est une fenêtre dans le BitVector32 et est composé du plus petit nombre de bits consécutifs pouvant contenir la valeur maximale spécifiée dans CreateSection. Par exemple, une section avec une valeur maximale de 1 est composée d’un seul bit, tandis qu’une section avec une valeur maximale de 5 est composée de trois bits. Vous pouvez créer un BitVector32.Section avec une valeur maximale de 1 pour servir de booléen, ce qui vous permet de stocker des entiers et des booléens dans le même BitVector32.

Certains membres peuvent être utilisés pour un BitVector32 qui est configuré en tant que sections, tandis que d’autres membres peuvent être utilisés pour un qui est configuré en tant qu’indicateurs de bits. Par exemple, la BitVector32.Item[] propriété est l’indexeur d’un BitVector32 qui est configuré en tant que sections, et la BitVector32.Item[] propriété est l’indexeur d’un BitVector32 qui est configuré en tant qu’indicateurs de bits. CreateMask crée une série de masques qui peuvent être utilisés pour accéder à des bits individuels dans un BitVector32 qui est configuré en tant qu’indicateurs de bits.

L’utilisation d’un masque sur un BitVector32 qui est configuré en tant que sections peut entraîner des résultats inattendus.

Constructeurs

BitVector32(BitVector32)

Initialise une nouvelle instance de la structure BitVector32 contenant les données représentées dans une structure BitVector32 existante.

BitVector32(Int32)

Initialise une nouvelle instance de la structure BitVector32 contenant les données représentées dans un entier.

Propriétés

Data

Obtient la valeur de BitVector32 sous la forme d'un entier.

Item[BitVector32+Section]

Obtient ou définit la valeur stockée dans le BitVector32.Section spécifié.

Item[Int32]

Obtient ou définit l'état de l'indicateur binaire déterminé par le masque spécifié.

Méthodes

CreateMask()

Crée le premier masque d'une série de masques qui peuvent être utilisés pour récupérer des bits dans un BitVector32 configuré en tant qu'indicateurs binaires.

CreateMask(Int32)

Crée un masque supplémentaire à la suite du masque spécifié d'une série de masques qui peuvent être utilisés pour récupérer des bits dans un BitVector32 configuré en tant qu'indicateurs binaires.

CreateSection(Int16)

Crée le premier BitVector32.Section d'une série de sections qui contiennent de petits entiers.

CreateSection(Int16, BitVector32+Section)

Crée un nouveau BitVector32.Section à la suite du BitVector32.Section spécifié dans une série de sections qui contiennent de petits entiers.

Equals(BitVector32)

Indique si l’instance actuelle est égale à une autre instance du même type.

Equals(Object)

Détermine si l'objet spécifié est égal à BitVector32.

GetHashCode()

Sert de fonction de hachage pour BitVector32.

ToString()

Retourne une chaîne qui représente le BitVector32 actuel.

ToString(BitVector32)

Retourne une chaîne qui représente le BitVector32 spécifié.

S’applique à

Voir aussi