VBspeed / Bits / ShiftRight
VBspeed © 2000-10, updated: 01-Oct-2001
ShiftRight OLD


NOTE --- 09-Oct-2001
All codes featured below go WRONG in most of the cases where Value is negative.
For example, ShiftRight(-3,1) should return -2 (not -1):
                            11111111111111111111111111111101          -3 
ShiftRight(-3, 1) ->        11111111111111111111111111111110          -2 
The charts and codes below are presented only for internal reference (learning from mistakes, you know). Corrected versions here: ShiftRight.


The Definition
Function ShiftRight
Shifts the bits to the right the specified number of positions and returns the new value.
Bits "falling off" the right edge do not wrap around. Fill bits coming in from left match bit 31 (the sign bit): if bit 31 is 1 the fill bits will be 1 (see ShiftRightZ for the alternative zero-fill-in version).

A shift right is effectively a division by 2. Some common languages like C/C++ or Java have an operator for this job: ">>".
Declaration
Function ShiftRight(Value As Long, ShiftCount as Long) As Long
Arguments
Valuevalue to use for operation
ShiftCountnumber of positions to shift, valid range is 0 thru 31.
You may use this function (VB5/6-compatible) to verify the correctness of your code.


The Charts
Calls
 lRet = ShiftRight(Value, ShiftCount)
Call 1 Value = 256
ShiftCount = 8
Call 2 Value = &H87654321
ShiftCount = 24
 VB5
CodeAuthorDopingNotes
ShiftRight01 Donald  
ShiftRight02 Donald  
ShiftRight03 Jost  
ShiftRight04 Jost  
Call 1
44.250.196µs
34.250.196µs
23.130.144µs
11.000.046µs
Call 2
34.260.196µs
44.260.196µs
23.140.144µs
11.000.046µs
 VB6
CodeAuthorDopingNotes
ShiftRight01 Donald  
ShiftRight02 Donald  
ShiftRight03 Jost  
ShiftRight04 Jost  
Call 1
33.900.179µs
43.900.179µs
23.130.144µs
11.000.046µs
Call 2
43.900.180µs
33.900.179µs
23.130.144µs
11.000.046µs
Conclusions
Well, VBspeed is proud to present another REAL fast VB-only code.
Mail your code! How to read all those numbers


The Code
ShiftRight01
submitted 18-Dec-2000 by Donald Lessau  
Doping: none
Public Function ShiftRight01(Value As Long, ByVal ShiftCount As Long) As Long
' by Donald, donald@xbeat.net, 20001218
  Select Case ShiftCount
  Case 0 To 31
    ShiftRight01 = Value \ Pow2(ShiftCount)
  End Select
End Function
Author's comments: See Pow2
Donald's comments:

top | charts


ShiftRight02
submitted 18-Dec-2000 by Donald Lessau  
Doping: none
Public Function ShiftRight02(Value As Long, ByVal ShiftCount As Long) As Long
' by Donald, donald@xbeat.net, 20001218
  
  If ShiftCount < 0 Or ShiftCount > 31 Then Exit Function
  
  ShiftRight02 = Value \ Pow2(ShiftCount)

End Function
Author's comments: See Pow2
Donald's comments:

top | charts


ShiftRight03
submitted 28-Sep-2001 by Jost Schwider    vb-tec.de
Doping: none
Public Static Function ShiftRight03(Value As Long, ByVal ShiftCount As Long) As Long
' by Jost Schwider, jost@schwider.de, 20010928
  Dim Pow2(0 To 31) As Long
  Dim i As Long
  
  Select Case ShiftCount
  Case 0 To 31
    'Ggf. Initialisieren:
    If i = 0 Then
      Pow2(0) = 1
      For i = 1 To 30
        Pow2(i) = 2 * Pow2(i - 1)
      Next i
    End If
    ShiftRight03 = Value \ Pow2(ShiftCount)
  End Select

End Function
Author's comments:
Donald's comments:

top | charts


ShiftRight04
submitted 01-Oct-2001 by Jost Schwider    vb-tec.de
Doping: none
Public Function ShiftRight04(ByVal Value As Long, ByVal ShiftCount As Long) As Long
' by Jost Schwider, jost@schwider.de, 20010928
  Select Case ShiftCount
  Case 0&:  ShiftRight04 = Value
  Case 1&:  ShiftRight04 = Value \ &H2&
  Case 2&:  ShiftRight04 = Value \ &H4&
  Case 3&:  ShiftRight04 = Value \ &H8&
  Case 4&:  ShiftRight04 = Value \ &H10&
  Case 5&:  ShiftRight04 = Value \ &H20&
  Case 6&:  ShiftRight04 = Value \ &H40&
  Case 7&:  ShiftRight04 = Value \ &H80&
  Case 8&:  ShiftRight04 = Value \ &H100&
  Case 9&:  ShiftRight04 = Value \ &H200&
  Case 10&: ShiftRight04 = Value \ &H400&
  Case 11&: ShiftRight04 = Value \ &H800&
  Case 12&: ShiftRight04 = Value \ &H1000&
  Case 13&: ShiftRight04 = Value \ &H2000&
  Case 14&: ShiftRight04 = Value \ &H4000&
  Case 15&: ShiftRight04 = Value \ &H8000&
  Case 16&: ShiftRight04 = Value \ &H10000
  Case 17&: ShiftRight04 = Value \ &H20000
  Case 18&: ShiftRight04 = Value \ &H40000
  Case 19&: ShiftRight04 = Value \ &H80000
  Case 20&: ShiftRight04 = Value \ &H100000
  Case 21&: ShiftRight04 = Value \ &H200000
  Case 22&: ShiftRight04 = Value \ &H400000
  Case 23&: ShiftRight04 = Value \ &H800000
  Case 24&: ShiftRight04 = Value \ &H1000000
  Case 25&: ShiftRight04 = Value \ &H2000000
  Case 26&: ShiftRight04 = Value \ &H4000000
  Case 27&: ShiftRight04 = Value \ &H8000000
  Case 28&: ShiftRight04 = Value \ &H10000000
  Case 29&: ShiftRight04 = Value \ &H20000000
  Case 30&: ShiftRight04 = Value \ &H40000000
  Case 31&: ShiftRight04 = Value \ &H80000000
  End Select
End Function
Author's comments:
Donald's comments:

top | charts




VBspeed © 2000-10 by Donald Lessau