ShiftLeft – Shift a Long to the left

' Shift to the left of the specified number of times'' NOTE: requires Power2()Function ShiftLeft(ByVal value As Long, ByVal times As Long) As Long    ' we need to create a mask of 1's corresponding to the    ' times 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 ShiftLeft = value: Exit Function        ' this extracts the bit in Value that will become the sign bit    mask = Power2(31 - times)    ' this calculates the sign bit of the result    signBit = CBool(value And mask) And &H80000000    ' this clears all the most significant times,    ' that would be lost anyway, and also clears the sign bit    value = value And (mask - 1)    ' do the shift to the left, without risking an overflow    ' and then add the sign bit    ShiftLeft = (value * Power2(times)) Or signBitEnd Function

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

Overview

Recent Articles: