开发者

In ASP, Bit Operator Left shift and Right shift

Does anyone know left shift and right shift operator sample's? I'm new in ASP.开发者_如何学运维 I found Bit operators such as AND,OR,NOT only..


For vbscript, left shift is accomplished by multiplication (i.e., var * 2 left shifts one position, var * 4 lefts shifts two positions, etc.) and right shift is accomplished by division (i.e., var \ 16 right shifts four positions).


There are no direct methods for left and right shift in vbscript, but as this is a simple move of each digit in a set of bits left or right, which can be done by dividing by binary 10 (integer 2), here are helper methods that do this

Function LeftShift(pValue, pShift)

Dim NewValue, PrevValue, i
PrevValue = pValue
For i = 1 to pShift
    Select Case VarType(pValue)
        Case vbLong
            NewValue = (PrevValue And "&H3FFFFFFF") * 2
            If PrevValue And "&H40000000" Then NewValue = NewValue Or "&H80000000"
            NewValue = CLng(NewValue)
        Case vbInteger
            NewValue = (PrevValue And "&H3FFF") * 2
            If PrevValue And "&H4000" Then NewValue = NewValue Or "&H8000"
            NewValue = CInt("&H"+ Hex(NewValue))
        Case vbByte
            NewValue = CByte((PrevValue And "&H7F") * 2)
        Case Else: Err.Raise 13 ' Not a supported type 
    End Select
    PrevValue = NewValue
Next
LeftShift = NewVAlue

End Function    

Function RightShift(pValue, pShift)

Dim NewValue, PrevValue, i
PrevValue = pValue
For i = 1 to pShift
    Select Case VarType(pValue)
        Case vbLong
            NewValue = Int((PrevValue And "&H7FFFFFFF") / 2)
            If PrevValue And "&H80000000" Then NewValue = NewValue Or "&H40000000"
            NewValue = CLng(NewValue)
        Case vbInteger
            NewValue = Int((PrevValue And "&H7FFF") / 2)
            If PrevValue And "&H8000" Then NewValue = NewValue Or "&H4000"
            NewValue = CInt(NewValue)
        Case vbByte
            NewValue = CByte(PrevValue / 2)
        Case Else: Err.Raise 13 ' Not a supported type
    End Select
    PrevValue = NewValue
Next
RightShift = PrevValue

End Function

For more, see http://chris.wastedhalo.com/2014/05/more-binarybitwise-functions-for-vbscript/


http://www.blackwasp.co.uk/CSharpShiftOperators.aspx

That's C# but the same operators work in recent versions of VB.Net.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜