BitVector32, structure
Assembly : System (dans system.dll)
BitVector32 est plus efficace que BitArray pour les valeurs booléennes et les petits entiers utilisés en interne. BitArray peut s'agrandir indéfiniment si nécessaire, mais possède la charge mémoire et la charge de performances requises pour une instance de classe. En revanche, BitVector32 utilise uniquement 32 bits.
Une structure BitVector32 peut être configurée afin de contenir des sections destinées aux petits entiers ou des indicateurs binaires pour des valeurs booléennes, mais pas les deux. BitVector32.Section est une fenêtre de BitVector32 composée du plus petit nombre de bits consécutifs qui peuvent contenir la valeur maximale spécifiée dans CreateSection. Par exemple, une section d'une valeur maximale de 1 n'est composée que d'un bit, tandis qu'une section d'une valeur maximale de 5 est composée de trois bits. Vous pouvez créer BitVector32.Section avec une valeur maximale de 1 pour l'utiliser comme valeur booléenne, ce qui vous permet de stocker des entiers et des valeurs booléennes dans le même BitVector32.
Certains membres peuvent être utilisés pour un BitVector32 configuré en tant que sections, tandis que d'autres membres peuvent être utilisés pour un autre configuré en tant qu'indicateurs binaires. Par exemple, la propriété BitVector32.Item est l'indexeur d'un BitVector32 configuré en tant que sections, et la propriété BitVector32.Item est l'indexeur d'un BitVector32 configuré en tant qu'indicateurs binaires. CreateMask crée une série de masques qui peuvent être utilisés pour accéder aux bits d'un BitVector32 configuré en tant qu'indicateurs binaires.
L'utilisation d'un masque sur un BitVector32 configuré en tant que sections peut entraîner des résultats inattendus.
L'exemple de code suivant utilise un BitVector32 en tant que collection d'indicateurs binaires.
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} */
import System.*;
import System.Collections.Specialized.*;
public class SamplesBitVector32
{
public static void main(String[] args)
{
// 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.set_Item(myBit1, true);
Console.WriteLine(" myBit1 = TRUE: {0}", myBV.ToString());
myBV.set_Item(myBit3, true);
Console.WriteLine(" myBit3 = TRUE: {0}", myBV.ToString());
myBV.set_Item(myBit5, true);
Console.WriteLine(" myBit5 = TRUE: {0}", myBV.ToString());
} //main
} //SamplesBitVector32
/*
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 en tant que collection de sections.
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 */
import System.*;
import System.Collections.Specialized.*;
public class SamplesBitVector32
{
public static void main(String[] args)
{
// 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((short)6);
BitVector32.Section mySect2 =
BitVector32.CreateSection((short)3, mySect1);
BitVector32.Section mySect3 =
BitVector32.CreateSection((short)1, mySect2);
BitVector32.Section mySect4 =
BitVector32.CreateSection((short)15, mySect3);
// Displays the values of the sections.
Console.WriteLine("Initial values:");
Console.WriteLine("\tmySect1: {0}",
System.Convert.ToString(myBV .get_Item( mySect1)));
Console.WriteLine("\tmySect2: {0}",
System.Convert.ToString(myBV.get_Item( mySect2)));
Console.WriteLine("\tmySect3: {0}",
System.Convert.ToString(myBV.get_Item(mySect3)));
Console.WriteLine("\tmySect4: {0}",
System.Convert.ToString(myBV.get_Item(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.set_Item(mySect1 , 5);
Console.WriteLine("\tmySect1 = 5:\t{0}", myBV.ToString());
myBV.set_Item(mySect2 , 3);
Console.WriteLine("\tmySect2 = 3:\t{0}", myBV.ToString());
myBV.set_Item(mySect3 , 1);
Console.WriteLine("\tmySect3 = 1:\t{0}", myBV.ToString());
myBV.set_Item(mySect4 , 9);
Console.WriteLine("\tmySect4 = 9:\t{0}", myBV.ToString());
// Displays the values of the sections.
Console.WriteLine("New values:");
Console.WriteLine("\tmySect1: {0}",
System.Convert.ToString(myBV.get_Item( mySect1)));
Console.WriteLine("\tmySect2: {0}",
System.Convert.ToString(myBV.get_Item(mySect2)));
Console.WriteLine("\tmySect3: {0}",
System.Convert.ToString(myBV.get_Item(mySect3)));
Console.WriteLine("\tmySect4: {0}",
System.Convert.ToString(myBV.get_Item(mySect4)));
} //main
} //SamplesBitVector32
/*
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
*/
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile pour Pocket PC, Windows Mobile pour Smartphone, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition
Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.