Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1448

Unsigned Subtraction for using timer without problem on overlap

$
0
0
The UnsignedSub(a, b) get two signed long, and return a sign long (has the same bits as the unsigned result).
For time duration, we can use Signed(a) which get a number and convert it to singed long, if we have a value grater than 2147483647 (more than 23 days).

Code:

Private Declare Sub GetMem4 Lib "msvbvm60" (ByVal Addr As Long, RetVal As Long)
Private Declare Sub PutMem4 Lib "msvbvm60" (ByVal Addr As Long, ByVal NewVal As Long)
Private Declare Sub PutMem2 Lib "msvbvm60" (ByVal Addr As Long, ByVal NewVal As Integer)
Function Signed(a) As Long
    Dim p
    p = CDec(Int(a))
    GetMem4 VarPtr(p) + 8, Signed
End Function
Public Function UnsignedSub(a As Long, b As Long)

    Static ua, ub
    If ua = Empty Then
        PutMem2 VarPtr(ua), 20
        PutMem2 VarPtr(ub), 20
    End If
    PutMem4 VarPtr(ua) + 8, a
    PutMem4 VarPtr(ub) + 8, b
    ua =ua - ub
    GetMem4 VarPtr(ua) + 8, UnsignedSub
End Function


Sub Main()
    Debug.Print UnsignedSub(-10, -50) = 40
    Debug.Print UnsignedSub(10, -30) = 40 ' overlap
    Debug.Print UnsignedSub(30, -10) = 40 ' overlap
    Debug.Print UnsignedSub(50, 10) = 40
    Debug.Print UnsignedSub(-10, 5) = -15
    Debug.Print UnsignedSub(-5, 10) = -15
    Debug.Print UnsignedSub(5, 20) = -15 ' overlap
    Debug.Print UnsignedSub(105, 120) = -15 ' overlap
    Debug.Print UnsignedSub(Signed(1234567878), Signed(2234567878#)) = -1000000000 '  overlap


    ' time now from timegettime to variable Z
    ' dt = 3000
    ' check: UnsignedSub(timegettime, z)>dt
   
End Sub


Viewing all articles
Browse latest Browse all 1448

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>