Results 1 to 1 of 1

Thread: Unsigned Subtraction for using timer without problem on overlap

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Unsigned Subtraction for using timer without problem on overlap

    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
    Last edited by georgekar; Feb 5th, 2023 at 04:17 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width