在ASP中,位操作员左移和右移

时间:2010-08-12 09:44:39

标签: asp-classic vbscript bitwise-operators

有谁知道左移和右移操作员样本? 我是ASP的新手。我找到了比特运算符,如AND,OR,不仅仅是..

3 个答案:

答案 0 :(得分:2)

对于vbscript,左移是通过乘法完成的(即,var * 2左移一个位置,var * 4左移两个位置,等等),右移是通过除法完成的(即var \ 16右移四位置)。

答案 1 :(得分:0)

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

这是C#,但是相同的运算符在最新版本的VB.Net中工作。

答案 2 :(得分:0)

vbscript中没有用于左右移动的直接方法,但是由于这是一组位中的每个数字向左或向右的简单移动,可以通过除以二进制10(整数2)来完成。是执行此操作的辅助方法

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

有关更多信息,请参见http://chris.wastedhalo.com/2014/05/more-binarybitwise-functions-for-vbscript/