TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
 Specialized Dev Zones Research Center eBook Library .NET Java C++ Web Dev Architecture Database Security Open Source Enterprise Mobile Special Reports 10-Minute Solutions DevXtra Blogs Slideshow

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

 Home » Tip Bank » .NET » VB.NET » Math Processing
Language: VB4,VB5,VB6,VBS
Expertise: Intermediate
Jun 26, 1999

### WEBINAR:On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning

# 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 signBit
End Function``````
Francesco Balena

 Submit a Tip Browse ".NET" Tips Browse All Tips
Comment and Contribute

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

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