devxlogo

ShiftLeft – Shift a Long to the left

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

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
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