Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

ShiftRight - Shift a Long to the right
[ All Languages » VB »  Math]

Total Hit ( 2514)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Click here to copy the following block
' 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

' Raise 2 to a power
' the exponent must be in the range [0,31]

Function Power2(ByVal exponent As Long) As Long
  Static res(0 To 31) As Long
  Dim i As Long
  
  ' rule out errors
  If exponent < 0 Or exponent > 31 Then Err.Raise 5
  
  ' initialize the array at the first call
  If res(0) = 0 Then
    res(0) = 1
    For i = 1 To 30
      res(i) = res(i - 1) * 2
    Next
    ' this is a special case
    res(31) = &H80000000
  End If
  
  ' return the result
  Power2 = res(exponent)
    
End Function


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.