提供了一个简单结构,该结构以32位内存存储布尔和小数值
BitVector32 只使用 32 位。
BitVector32 中存储整数和布尔值。
BitVector32 中的单个位。
BitVector32 上使用屏蔽可能会导致意外的结果。
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}
*/ BitVector用作节集合
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
*/
|
请发表评论