BitVector32 結構
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供在 32 位元記憶體中儲存布林值 (Boolean) 和小整數的簡單結構。
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)
- 繼承
- 實作
範例
下列程式代碼範例使用 BitVector32 做為位旗標的集合。
#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}
下列程式代碼範例使用 BitVector32 做為區段的集合。
#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
備註
BitVector32 比 BitArray 內部使用的布爾值和小型整數更有效率。 BitArray可以視需要無限期成長,但它具有類別實例所需的記憶體和效能額外負荷。 相反地, BitVector32 只會使用32位。
BitVector32結構可以設定為包含小整數的區段或布爾值的位旗標,但不能同時包含兩者。 BitVector32.Section是的BitVector32視窗,由最小數目的連續位組成,可包含 中指定的CreateSection最大值。 例如,最大值為1的區段只包含一個位,而最大值為5的區段則由三位組成。 您可以建立最大值為 1 的 , BitVector32.Section 以做為布爾值,因此可讓您將整數和布爾值儲存在相同的 BitVector32中。
某些成員可用於 BitVector32 設定為區段的 ,而其他成員則可用於設定為位旗標的成員。 例如, BitVector32.Item[] 屬性是設定為區段之 的索引器 BitVector32 ,而 BitVector32.Item[] 屬性是設定為位旗標之 的 BitVector32 索引器。 CreateMask 會建立一系列的遮罩,可用來存取 設定為位旗標之 中的 BitVector32 個別位。
在設定為區段的 上使用 BitVector32 遮罩可能會導致非預期的結果。
建構函式
BitVector32(BitVector32) |
初始化 BitVector32 結構的新執行個體,該結構包含現有 BitVector32 結構中所表示的資料。 |
BitVector32(Int32) |
初始化 BitVector32 結構的新執行個體,該結構包含整數所表示的資料。 |
屬性
Data |
取得 BitVector32 的值做為整數。 |
Item[BitVector32+Section] |
取得或設定儲存於指定 BitVector32.Section 中的值。 |
Item[Int32] |
取得或設定由指定遮罩所指示的位元旗標狀態。 |
方法
CreateMask() |
建立一系列遮罩中的第一個遮罩,可以用於擷取設定為位元旗標之 BitVector32 中的個別位元。 |
CreateMask(Int32) |
在一系列遮罩中的指定遮罩之後建立額外的遮罩,可以用於擷取設定為位元旗標之 BitVector32 中的個別位元。 |
CreateSection(Int16) |
建立包含小整數之一系列區段中的第一個 BitVector32.Section。 |
CreateSection(Int16, BitVector32+Section) |
在包含小整數的一系列區段中,建立新 BitVector32.Section 於指定的 BitVector32.Section 之後。 |
Equals(BitVector32) |
指出目前的實例是否等於相同類型的另一個實例。 |
Equals(Object) |
判斷指定的物件和 BitVector32 是否相等。 |
GetHashCode() |
做為 BitVector32 的雜湊函式。 |
ToString() |
傳回表示目前 BitVector32 的字串。 |
ToString(BitVector32) |
傳回字串,表示指定的 BitVector32。 |