2010-12-23 5 views
1

내가제대로

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) 

이 그리고 나는 단지 비트 플래그에 대한 BitVector32를 사용 싶었고로 출력을 원하는

0b00000011000000110000001100000011 

를 얻을 수 bitvector32 사용하는 방법

0b00000000000000000000000000011001 

어떻게 이것을 올바르게 설정합니까?

답변

2

BitVector32의 인수는 마스크이며 비트 오프셋이 아닙니다. BitArray은 아마도 당신이 원하는 것에 더 가깝습니다.

(편집) hmmm - 아마도 가장 쉬운 방법은 시프트 연산자를 사용하는 것입니다. C#의 경우 :

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

인덱스 0은 LSB를 참조하므로 루프를 되돌려 야합니다.

또는 더 쉽게 :

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

내가 SB하는 라인을 변경하여 IIf = & (BV (Specialized.BitVector32.CreateMask (I)), 1, 0) 및 출력 0b00000000000000000001000100010001이다. 여전히 옳지 않습니다. 또한 성능 현명한, 난 32 비트 정수와 비트 마스크가 필요합니다, 당신은 여전히 ​​bitarray을 권 해드릴까요? –

+0

실제로 비트로 연주해야합니다 (이 전에 비트 연산이 몇 가지 있습니다), 문제를 더 쉽게 표시 할 수 있도록 문자열로 변환했습니다. –

+0

@Herp - 아니요,'int' aka'Int32'와'Convert.ToString (...) '을 사용하는 것이 좋습니다 ... –

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