Results 1 to 2 of 2

Thread: CInt64() How to use it.

  1. #1

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

    CInt64() How to use it.

    Using integer division on long long works.
    Here is the output from this module.
    You can use long long for calculations.
    See the power function. Also I found that 2^63 return negative, the actual unsigned number in bits &H8000000000000000



    12345678912345678 Long Long
    6172839456172839 Long Long
    4611686018427387904 = 2^62
    4000000000000000
    3737184779924949 Long Long
    8000000000000000
    9223372036854775807
    -9223372036854775808
    4294967295

    Code:
    Private Declare Sub GetMem2 Lib "msvbvm60" (ByVal Addr As Long, retval As Integer)
    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)
    Private Declare Sub PutMem1 Lib "msvbvm60" (ByVal Addr As Long, ByVal NewVal As Byte)
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
        lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
    Private maxlonglong, limitlonglong
    Sub main()
    maxlonglong = CDec("18446744073709551616")
    limitlonglong = CDec("9223372036854775808")
    Dim a, b
    a = cInt64("12345678912345678")
    Debug.Print a, VarTypeName(a)
    Debug.Print a \ 2, VarTypeName(a \ 2)
    b = cInt64(123)
    a = IntExp(cInt64(2), cInt64(62), OneLongLong)  ' 2^62  (2^63 return right bits for unsigned, but as negative signed)
    Debug.Print a, " = 2^62"
    Debug.Print Hex64(a)
    Debug.Print a \ cInt64(1234), VarTypeName(a \ cInt64(1234))
    'a = OneLongLong
    a = IntExp(cInt64(2), cInt64(63), OneLongLong)
    Debug.Print Hex64(a)
    Debug.Print cInt64("&H7FFFFFFFFFFFFFFF")
    Debug.Print cInt64("&H8000000000000000")
    Debug.Print cInt64("&H00000000FFFFFFFF") ' always feed the zero to get unsigned long as long long
    
    
    
    End Sub
    Public Function myVarType(z, j As Integer) As Boolean
    Dim i As Integer
    GetMem2 VarPtr(z), i
    myVarType = i = j
    End Function
    Public Function IntExp(n, x, ByVal R)
    On Error GoTo 100
        While x > 0: If x Mod 2 = 1 Then x = x - 1: R = R * n
        x = x \ 2: If x > 0 Then n = n * n
        Wend
        IntExp = R
        Exit Function
    100: Err.Clear
        R = 0.5
    End Function
    Public Function VarTypeName(v) As String
    Dim n As Integer
    GetMem2 VarPtr(v), n
    
    
        Select Case n And &H7FF
            Case 0
                VarTypeName = "Empty"
            Case 1
                VarTypeName = "Null"
            Case 2
                VarTypeName = "Integer"
            Case 3
                VarTypeName = "Long"
            Case 4
                VarTypeName = "Single"
            Case 5
                VarTypeName = "Double"
            Case 6
                VarTypeName = "Currency"
            Case 7
                VarTypeName = "Date"
            Case 8
                VarTypeName = "String"
            Case 10
                VarTypeName = "Error"
            Case 9, 13
                VarTypeName = TypeName(v)
            Case 11
                VarTypeName = "Boolean"
            Case 12, 1, 36
                VarTypeName = "Variant"
            Case 14
                VarTypeName = "Decimal"
            Case 17
                VarTypeName = "Byte"
            Case 20
                VarTypeName = "Long Long"
            Case 8204
                VarTypeName = "Variant()"
            Case Is > 8000
            On Error GoTo 1000
                If UBound(v) > LBound(v) Then
                    VarTypeName = VarTypeName(v(LBound(v))) + "()"
                    If VarTypeName = "Nothing()" Then VarTypeName = "Object()"
                    
                Else
    1000         If Err.Number <> 0 Then Err.Clear
                VarTypeName = "Array"
                End If
                
            Case Else
                VarTypeName = "type" & VarType(v)
        End Select
    End Function
    Public Function HighLong(a) As Long
        HighLong = LowLong(cInt64(a) \ OneBigLongLong())
    End Function
    Public Function LowLong(ByVal p) As Long
        If Not myVarType(p, 20) Then p = cInt64(p)
        CopyMemory ByVal VarPtr(LowLong), ByVal VarPtr(p) + 8, 4
    End Function
    Function Hex64$(a, Optional showlong)
        Dim p, p1, sg As Integer
        a = cInt64(a)
        sg = Sgn(a)
        p = -OneLongLong() And a
        p1 = p \ OneBigLongLong()
        p1 = LowLong(p1)
        p = LowLong(p)
        If Not IsMissing(showlong) Then If showlong Then sg = Sgn(p)
        
        If p1 = 0 And sg = -1 Then
            Hex64$ = "FFFFFFFF" + Right$("0000000" + Hex$(p), 8)
        Else
            Hex64$ = Right$("0000000" + Hex$(p1), 8) + Right$("0000000" + Hex$(p), 8)
        End If
    End Function
    Public Function OneLongLong() As Variant
        Static p
        If p = Empty Then
            PutMem2 VarPtr(p), 20
            PutMem1 VarPtr(p) + 8, 1
        End If
        OneLongLong = p
    End Function
    Public Function OneBigLongLong() As Variant
        Static p
        If p = Empty Then
            PutMem2 VarPtr(p), 20
            PutMem1 VarPtr(p) + 12, 1
        End If
        OneBigLongLong = p
    End Function
    Public Function MaskLowLongLong() As Variant
        PutMem2 VarPtr(OneBigLongLong), 20
        PutMem4 VarPtr(OneBigLongLong) + 12, -1&
    End Function
    Public Function cInt64(p)
        Dim a
        Dim i As Integer
        GetMem2 VarPtr(p), i
        Select Case i
        Case vbDecimal
            a = Fix(p)
            If a < -limitlonglong - 1 Then
                While a <= -limitlonglong - 1: a = a + maxlonglong: Wend
            End If
            While a > limitlonglong: a = a - maxlonglong: Wend
            cInt64 = -OneLongLong() And a
        Case 20
            cInt64 = p
        Case vbLong, vbInteger
            cInt64 = -OneLongLong() And p
        Case Else
            a = Fix(CDec(p))
            If a <= -limitlonglong - 1 Then
                While a <= -limitlonglong - 1: a = a + maxlonglong: Wend
            End If
            While a > limitlonglong: a = a - maxlonglong: Wend
            cInt64 = -OneLongLong() And a
            If i = vbString Then
                If Left$(p, 1) = "&" And a < 0 Then
                If Len(p) = 10 Then
                If InStr("89ABCDEF", UCase(Mid$(p, 3, 1))) = 1 Then
                cInt64 = OneBigLongLong() + cInt64
                End If
                Else
                If InStr("89ABCDEF", UCase(Mid$(p, 3, 1))) = 0 Then
                cInt64 = OneBigLongLong() + cInt64
                End If
                End If
                End If
            End If
        End Select
    End Function

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,263

    Re: CInt64() How to use it.

    You've posted this in the VB.NET CodeBank. Based on the code, it appears that it should have been in the VB6 CodeBank. If so, please use the Report Post to send a message to the mods to ask them to move it.

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