devxlogo

ShiftRight – Shift a Long to the right

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

See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist