Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB4,VB5,VB6,VBS
Expertise: Intermediate
Jun 26, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

ShiftRight - Shift a Long to the right

' Shift to the right of the specified number of times
' NOTE: requires Power2()

Function ShiftRight(ByVal value As Long, ByVal times As Long) As Long
    ' we need to create a mask of 1's corresponding to the
    ' digits in VALUE that will be retained in the result
    Dim mask As Long, signBit As Long
    ' return zero if too many times
    If times >= 32 Then Exit Function
    ' return the value if zero times
    If times = 0 Then ShiftRight = value: Exit Function
    ' evaluate the sign bit in advance
    signBit = (value < 0) And Power2(31 - times)
    ' create a mask with 1's for the digits that will be preserved
    If times < 31 Then
        ' if times=31 then the mask is zero
        mask = Not (Power2(times) - 1)
    End If
    ' clear all the digits that will be discarded, and
    ' also clear the sign bit
    value = (value And &H7FFFFFFF) And mask
    ' do the shift, without any problem, and add the sign bit
    ShiftRight = (value \ Power2(times)) Or signBit
End Function
Francesco Balena
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date