Hi Rudolf,
There's an important but very unobvious flaw in your routine!!
Private Function ShiftL_RealDiv (ByVal val As Integer, _
ByVal bits As Integer) As Integer
If bits = 0 Then Return 0
Return CType (val / (2 ^ bits), Integer)
End Function
vs
Private Function ShiftL_IntDiv (ByVal val As Integer, _
ByVal bits As Integer) As Integer
If bits = 0 Then Return 0
Return val \ CInt (2 ^ bits)
End Function
This is the output of a loop doing ShiftL (I, 1):
I = 0, ShiftL_Int = 0, ShiftL_Float = 0
I = 1, ShiftL_Int = 0, ShiftL_Float = 0
I = 2, ShiftL_Int = 1, ShiftL_Float = 1
I = 3, ShiftL_Int = 1, ShiftL_Float = 2 x
I = 4, ShiftL_Int = 2, ShiftL_Float = 2
I = 5, ShiftL_Int = 2, ShiftL_Float = 2
I = 6, ShiftL_Int = 3, ShiftL_Float = 3
I = 7, ShiftL_Int = 3, ShiftL_Float = 4 x
I = 8, ShiftL_Int = 4, ShiftL_Float = 4
I = 9, ShiftL_Int = 4, ShiftL_Float = 4
I = 10, ShiftL_Int = 5, ShiftL_Float = 5
I = 11, ShiftL_Int = 5, ShiftL_Float = 6 x
I = 12, ShiftL_Int = 6, ShiftL_Float = 6
I = 13, ShiftL_Int = 6, ShiftL_Float = 6
I = 14, ShiftL_Int = 7, ShiftL_Float = 7
I = 15, ShiftL_Int = 7, ShiftL_Float = 8 x
As you can see the integer division has the expected pairs while the
floating division produces singlets and triples.
The difference is due to the way that VB now rounds floating point
division to the nearest even number (round-to-even). It's to help with
accuracy in floating point arithmetic but it really screws things up when
doing integer arithmetic. It's one that you have to watch out for as it's a
real hidden bug producer. $%£&*!! on the person who decided that this was the
way to go.
Regards,
Fergus
ps. If bits = 0 Then Return 0
Why do you return 0? A shift of 0 bits should return the original number,
surely?