Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript Shift Left alternative 1

Status
Not open for further replies.

MarkZK

Technical User
Jul 13, 2006
202
GB
Hi,

Could somebody show me how I "should" be writing this VB script that is meant to be a replacement for the Javascript "shift left" operator, here is the code I've been using to test it,

Code:
<script language="JavaScript">
function leftshiftjs(op,n) {
    alert( op << n )
}
</script>
<script language="VBScript">
Function leftshiftvb(op,n)
 Dim shiftleft, i
 shiftleft = op
  For i = 0 To n - 1
  shiftleft = shiftleft * 2 
  Next  
msgbox(shiftleft)
End Function 
</script>
<button onclick="leftshiftvb(2,46)">TestVB</button>
<br>
<button onclick="leftshiftjs(2,46)">TestJS</button>

it seems to only work correctly with very low values, then the higher the values the more the VBScript differs from the Javascripts until it overflow errors out.

Thanks.
 
Msgbox 2 ^ 26 '???

or would it be ... shiftleft * (2 ^ 26)
 
Hello MrMovie,

I was hoping there was a perfect alternative to the Javascript function, so, I guess the answer would be as big as the Javascript values can go (not sure what that is).

Thanks for the replies.
 
Your vbscript function is doing this:
msgbox op * (2^n)
2 * (2^46) = 2^47

Left shift does this:
2 = 10 in binary
10 shifts to the 46th position.
1000000000000000000000000000000000000000000000
2^47

Here's where you problem is (vbs and jscript)
leftShift(2,29) = 1073741824 and 1073741824
leftShift(2,30) = 2147483648 and -2147483648
leftShift(2,31) = 4294967296 and 0
leftShift(2,32) = 8589934592 and 2

So basically, at 2,30 your results no longer match. At 2,31 it appears generates 0s. And 2,32 should be like 2,1.
 
On a 32-bit system JavaScript's << operator deals with 32-bit signed integers. The <<< operator deals with 32-bit unsigned integers.

Johnwm showed you one method of dealing with 16-bit signed integers in thread708-1320914. You just need to modify it to deal with 32-bit numbers.
 
Code:
Function leftshiftvb(op,n)
    Dim i, iHalf, iBinary, sBinary, iDec
    While n > 31
        n = n - 32
    Wend
    If op > 1 Then
        Do While True
            iHalf = Int(op / 2)
            iTemp = op - (iHalf*2)
            sBinary = cStr(iTemp) & sBinary
            op = iHalf
            If op = 0 Then Exit Do
        Loop
    ElseIf op = 1 Then
        sBinary = "1"
    End If
    For i = 1 to n
        sBinary = sBinary & "0"
    Next
    If Len(sBinary) > 32 Then
        sBinary = Right(sBinary,32)
    End If
    leftShift = 0
    For i = 1 To Len(sBinary)
        If Len(sBinary) <> 32 Or i <> 1 Then
            iPwr = Len(sBinary)-i
            iDec = (cSng(Mid(sBinary,i,1)) * 2^iPwr)
            leftShift = leftShift + iDec
        End If
    Next
    If Len(sBinary) = 32 And Left(sBinary,1) = "1" Then
        If leftShift = 0 Then
            leftShift = "-2147483648"
        Else
            leftShift = leftShift * -1
        End If
    End If
    MsgBox leftShift
End Function

This appears to work for the numbers I've tested. It coverts the number to binary. Then it takes the binary and converts it to a numeric.
 
Hi all,

changing to 32-bit only removed the overflow errors, the actual values returned would still differ from the Javascript values as Skie explained.

Thanks for that function Skie, I knew it had to be more complex than I could have dealt with, all seems to be working well now.

Markdmac, if you click the thread Strongm posted you'll see that I have to replace the Javascripts leftshift in order to compile the GeoIP to a DLL file.

Thanks again all.
 
SO you do still actually want a VB solution rather than VBScript ...

Check the original thread out again. I've posted a couple of possible solutions
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top