I’m porting a code and I thought Bitwise.ShiftRight was what I’m trying to port but it only gives me the same answer if the number is positive, so now I don’t know if
A) it’s my lack of porting abilities
B) it isn’t a Bitwise.ShiftRight
C) Bitwise.ShiftRight doesn’t handle negative numbers (or many other options)
This is how I translated a VB6 ShiftRight and if lValue is a positive number it’s the same as Bitwise.ShiftRight but not if it’s a negative number
[code] Dim ReturnValue As Integer
If iShiftBits = 0 Then
Return lValue
ElseIf iShiftBits = 31 Then
If (lValue And &H80000000) <> 0 Then
ReturnValue = 1
Else
ReturnValue = 0
End If
Return ReturnValue
ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
//Error but it seems to be 24 or 0 everytime
End If
ReturnValue = (lValue And &H7FFFFFFE) \ m_l2Power(iShiftBits)//array of 2^
If (lValue And &H80000000) <> 0 Then
ReturnValue = (ReturnValue Or (&H40000000 \ m_l2Power(iShiftBits - 1)))
End If
Return ReturnValue[/code]
Bitwise operators don’t care (or know) about the “sign”… they operate on the bits only, and a negative number is stored as a “1’s complement” (or is it 2’s complement, I never remember) format
+1 as a 16bit integer is 0000 0000 0000 0001
-1 as a 16 bit integer is 1111 1111 1111 1111
but those same bit values (well the negative one anyways) is a totally different value if it is a UInt instead of an Integer
If Xojo had arithmetic shift left and arithmetic shift right operations (as opposed to logical shift operations), then the sign would be properly taken care of. Lacking that, you would be further ahead simply to divide by the appropriate power of 2 for a right shift and multiply by power of 2 for left shift:
n=numberToBeShifted/2^nPositions 'for arithmetic right shift
n=numberToBeShifted*2^nPositions 'for arithmetic left shift
Thanks but I still don’t understand what I’ve done wrong porting the VB6 code
Bitwise.ShiftRight(-2047271774, 8) = -7997156
-2047271774/(2^8) = -7997156
my code = 8780060
I Googled the VB6 shift function and saw some uncomplimentary comments about how the shift is handled. I can’t claim I read enough to understand it, but I believe the problem you are having is just because the bitwise shift functions operate differently in VB6 and XOJO. So if you need to convert code, I suggest just replacing the VB6 shift code with the multiplication equivalent: in your example -2047271774/(2^8) = -7997156.
Thanks, I know the vb6 code works as far as encrypting and decrypting, so I guess I’ll just have to replace all of it and see if it works with Xojo code.
[quote=298679:@Jym Morton]Thanks but I still don’t understand what I’ve done wrong porting the VB6 code
Bitwise.ShiftRight(-2047271774, 8) = -7997156
-2047271774/(2^8) = -7997156
my code = 8780060[/quote]
It’s quite clear from these results that VB6 does an arithmetic shift whereas Xojo does a logical shift.
Edit:
One point that I overlooked is that there may be a difference between an arithmetic right shift and division by power of 2 when dealing with negative numbers. This is explained in the linked article. I suggest you test the VB6 code to see what happens when you do a right shift of -1.
@Robert Weaver
Looking at the Binary in the debugger however it looks like VB code does the logic shift and Xojo does the Arithmetic Shift.
-1 for bitwise.Shift Right ends up a hole pile of 1s and according to that Wiki page that’s what would happen with a Right Arithmetic Shift and having 8 zeros out the front (16777215) would be the logical Shift Right as the MSB is replaced with a zero for each move to the right.
Sorry, I got things a bit mixed up earlier when I looked at your results. If you right shift a negative number and the result is negative then that indicates that it was an arithmetic shift. A logical right shift will always produce a positive number, because a zero will enter the most significant bit position, and that is the sign bit. If you need to exactly duplicate the VB6 operation, then you’ll probably have to add a bit more code. You could do a bitwise right shift followed by a bitwise AND to zero the upper bits.
I have all 31 in an array so it’s pretty simple with any shift. It’s funny because I need to draw it on graph paper to figure out any shift, AND, OR. Not looking forward to my next Method, it’s got Xor that sounds like fun
@Robert Weaver I’ve put in about 100 positive numbers and they seem to be unaffected by the AND. Is this always true so I don’t have have to run an if statement to test the number being shifted?
if LValue > 0 then