Dear visitor, welcome to Jabaco - Community. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.
|
|
Jabaco Source |
1 2 3 4 5 6 |
Public Function ShL(value As Integer, shifter As Byte) As Long ShL = value * 2 ^ shifter End Function Public Function ShR(value As Integer, shifter As Byte) As Long ShR = value / 2 ^ shifter End Function |
|
|
Jabaco Source |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Private sh() As Integer = (&H1, &H2, &H4, &H8, _ &H10, &H20, &H40, &H80, _ &H100, &H200, &H400, &H800, _ &H1000, &H2000, &H4000, &H8000, _ &H10000, &H20000, &H40000, &H80000, _ &H100000, &H200000, &H400000, &H800000, _ &H1000000, &H2000000, &H4000000, &H8000000, _ &H10000000, &H20000000, &H40000000, &H80000000) Public Function ShL(value As Integer, shifter As Byte) As Long ShL = value * sh(shifter) End Function Public Function ShR(value As Integer, shifter As Byte) As Long ShR = value / sh(shifter) End Function |
Date of registration: Jan 1st 2009
Location: Hanover, Germany
Occupation: Software Engineer
Hobbies: Hilbert Curves
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
Option Explicit
Public sh(0 To 15) As Integer = ( &H1, &H2, &H4, &H8, _
&H10, &H20, &H40, &H80, _
&H100, &H200, &H400, &H800, _
&H1000, &H2000, &H4000, &H8000, _
&H10000, &H20000, &H40000, &H80000, _
&H100000, &H200000, &H400000, &H800000, _
&H1000000, &H2000000, &H4000000, &H8000000, _
&H10000000, &H20000000, &H40000000, &H80000000)
Public Function ShL(value As Integer, shifter As Byte) As Long
ShL = value * sh(shifter)
End Function
Public Function ShR(value As Integer, shifter As Byte) As Long
ShR = value / sh(shifter)
End Function
Public Sub shTestAll
Dim i As Integer
Dim p As Integer
p = 1
For i = 0 To 15
' debug.Print i & ": " & i2b(sh(i))
sh(i) = p
p = 2 * p
Next i
shTest "0000000000000001", 1, False, "0000000000000010"
shTest "0000000000000010", 1, True, "0000000000000001"
shTest "1000000000000000", 1, True, "0100000000000000"
shTest "1000000000000011", 2, True, "0010000000000000"
End Sub
Private Sub shTest(value As String, shifter As Byte, bToRight As Boolean, result As String)
Dim v As Integer = b2i(value)
Dim r As Integer = b2i(result)
Dim ret As Integer
If bToRight Then
ret = ShR(v, shifter)
Else
ret = ShL(v, shifter)
End If
If ret <> r Then
debug.Print value & iif(bToRight, " >> ", " << ") & CStr(shifter) & " = " & i2b(ret) & " <> " & result & " Error!"
Else
debug.Print value & iif(bToRight, " >> ", " << ") & CStr(shifter) & " = " & result & " OK!"
End If
End Sub
Private Function b2i(b As String) As Integer
Dim ret As Integer = 0
Dim c As String
Dim pos As Integer
For pos = 1 To len(b)
c = mid(b, pos, 1)
ret = 2 * ret + (c = "1")
Next pos
b2i = ret
End Function
Private Function i2b(i As Integer) As String
Dim v As Integer = i
Dim ret As String = ""
Do While v <> 0
ret = iif(v Mod 2 = 1, "1", "0") & ret
v = v / 2
Loop
i2b = iif(ret = "", "0", ret)
End Function
|
|
|
Jabaco Source |
1 |
Public sh(0 To 15) As Integer = (...) |
|
|
Jabaco Source |
1 |
Public sh() As Integer = (...) |