VB 2005 BitVector32

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I need some help on the BitVector32 class.

I'm trying to create a function like:

Private Sub Function (ByVal Input as integer, ByVal Bitnr as integer,
ByVal BitHigh as Boolean) As Integer

Return Output
End Function

The function must return a new integer value, with the proper bitnumber set
true or false.

Thanks

Hans
 
Hello Hans,

you can do this with this function:

Private Shared Function IsFlagSet(ByVal value As Integer, ByVal bitNr As
Integer) As Boolean
If ((bitNr < 0) OrElse (bitNr > 31)) Then
Throw New ArgumentOutOfRangeException("bitNr")
End If
Return (((value >> bitNr) And 1) = 1)
End Function

Best regards,
Henning Krause
 
Hello Henning,

Thanks for your resaction,

But i don't want to know if the bit is set or not.
Sample:

Private Sub Function SetBitInt32 (ByVal Input as integer, ByVal Bitnr as
integer,
ByVal BitHigh as Boolean) As Integer

Dim Output as Integer
Dim BV As New BitVector32(Input)

" Then set the Bitnr in the Input , BitHigh True or False"
" Return the Output Integer"

Return Output

End Function

I would like to use the BitVector32 class.


Best regards,
Hans
 
Hello,

you can do this with your own function like this:

Private Shared Function SetBit(ByVal value As Integer, ByVal bitNr As
Integer, ByVal state As Boolean) As Integer
If ((bitNr < 0) OrElse (bitNr > &H1F)) Then
Throw New ArgumentOutOfRangeException("bitNr")
End If
Dim mask As Integer = (CInt(1) << bitNr)
If state Then
value = (value Or mask)
Else
value = (value And Not mask)
End If
Return value
End Function

Best regards,
Henning Krause
 
Hello,

Thanks,

Your function works great.

But i would like to know how the bitvector32 class works.
Can i have the same functionality with that class ?


Best Regards,

Hans
 
Hello Henning,

Thanks for putting me in the right direction.

This is my solution with the bitvector32 class.


Public Function SetInt32Bit(ByVal iInputInt32 As Integer, ByVal iBitNr
As Integer, ByVal State As Boolean) As Integer

Dim output As Integer

Dim myBit(31) As Integer

For i As Integer = 0 To 31
If i = 0 Then
myBit(i) = BitVector32.CreateMask()
Else
myBit(i) = BitVector32.CreateMask(myBit(i - 1))
End If
Next

If State = True Then
output = iInputInt32 Or myBit(iBitNr)
Else
output = iInputInt32 And Not myBit(iBitNr)
End If


Return output

End Function

Best regards,

Hans
 
Back
Top