开发者

How to use bitvector32 properly

I have

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)

And I get

0b00000011000000110000001100000011

I only wanted to开发者_开发百科 use BitVector32 for bit flags and I wanted the output to be

0b00000000000000000000000000011001

How do I set this up properly?


The argument to BitVector32 is a mask, not a bit offset. BitArray is probably closer to what you want.

(edit) hmmm - maybe the easiest thing is to use shift operators; in C#:

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

Also note that index 0 refers to the LSB - so you need to reverse the loop.

Or easier:

Convert.ToString(25, 2).PadLeft(32, '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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜