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 signBitEnd Function

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: