Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


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

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 signBit
End Function
Francesco Balena
 
Comment and Contribute

 

 

 

 

 


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

 

 

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