如何正确使用bitvector32

时间:2010-12-23 09:57:44

标签: .net vb.net

我有

Dim bv As New Specialized.BitVector32(25)
Dim sb As String = "0b" 'I resisted the urge to use a stringbuilder

For i As Integer = 31 To 0 Step -1
    sb &= IIf(bv(i), 1, 0)
Next

Console.WriteLine(sb)

我得到了

0b00000011000000110000001100000011

我只想将BitVector32用于位标志,我希望输出为

0b00000000000000000000000000011001

如何正确设置?

2 个答案:

答案 0 :(得分:2)

BitVector32的参数是掩码,而不是偏移量。 BitArray可能更接近你想要的东西。

(编辑)嗯 - 也许最简单的方法是使用移位运算符;在C#中:

    for(int i = 31 ; i >= 0; i--) {
        sb += bv[1 << i] ? "1" : "0";
    }

另请注意,索引0指的是LSB - 因此您需要反转循环。

或者更容易:

Convert.ToString(25, 2).PadLeft(32, '0');

答案 1 :(得分:0)

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) Handles Button1.Click

        '
        Dim myBits As New Bits()
        myBits.BitSetVal(25)
        Debug.WriteLine(myBits.toBitString(""))
        myBits.BitClearVal(25)

        For x As Integer = 0 To 31
            myBits.BitSet(x)
            Debug.WriteLine(myBits.BitVal(x).ToString("n0"))
        Next
        Debug.WriteLine(myBits.toBitString("", 4))

        For x As Integer = 31 To 0 Step -1
            myBits.BitClear(x)
            Debug.WriteLine(myBits.toBitString("", 4))
        Next

    End Sub
End Class

Class Bits
    Dim _theBits As Integer
    Const _allOnes As Integer = -1

    Public Sub New()
        Me._theBits = 0
    End Sub

    Public Sub New(ByVal initialValue As Integer)
        Me._theBits = initialValue
    End Sub

    Public Sub BitSet(ByVal theBitToSet As Integer) 'set one bit
        If theBitToSet < 0 OrElse theBitToSet > 31 Then throwRangeException("set")
        Dim foo As Integer = 1 << theBitToSet
        Me._theBits = Me._theBits Or foo
    End Sub

    Public Sub BitSetVal(ByVal theValToSet As Integer) 'set a value
        Me._theBits = Me._theBits Or theValToSet
    End Sub

    Public Sub BitSetAll() 'set all bits
        Me._theBits = _allOnes
    End Sub

    Public Sub BitClear(ByVal theBitToClear As Integer) 'clear one bit
        If theBitToClear < 0 OrElse theBitToClear > 31 Then throwRangeException("clear")
        Dim foo As Integer = (1 << theBitToClear) Xor _allOnes
        Me._theBits = Me._theBits And foo
    End Sub

    Public Sub BitClearVal(ByVal theValToClear As Integer) 'clear bit value
        Dim foo As Integer = theValToClear Xor _allOnes
        Me._theBits = Me._theBits And foo
    End Sub

    Public Sub BitClearAll() 'clear all bits
        Me._theBits = 0
    End Sub

    Public Function BitGet(ByVal theBitToGet As Integer) As Boolean 'get bit state
        If theBitToGet < 0 OrElse theBitToGet > 31 Then throwRangeException("get")
        Dim foo As Integer = 1 << theBitToGet
        If (Me._theBits And foo) = foo Then Return True Else Return False
    End Function

    Public Function BitVal(ByVal theBitToGet As Integer) As Integer 'return the value of a bit
        If theBitToGet < 0 OrElse theBitToGet > 31 Then throwRangeException("val")
        Dim foo As Integer = 1 << theBitToGet
        Return Me._theBits And foo
    End Function

    Public Function toBitString(Optional ByVal FormatString As String = "", Optional ByVal Spacing As Integer = 0) As String
        Dim sb As New System.Text.StringBuilder
        If FormatString = "" Then
            sb.Append(Convert.ToString(Me._theBits, 2).PadLeft(32, "0"c))
        ElseIf FormatString.ToLower = "h" Then
            sb.Append(Convert.ToString(Me._theBits, 16).PadLeft(8, "0"c))
        End If

        If Spacing <> 0 Then
            For z As Integer = sb.Length - Spacing To Spacing Step -Spacing
                sb.Insert(z, " ")
            Next
        End If
        Return sb.ToString

    End Function

    Private Sub throwRangeException(ByVal who As String)
        Throw New ArgumentException(String.Format("Bit to {0} must be >= 0 and <= 31", who))
    End Sub
End Class