OverFlow problem

Posted on 2004-11-12
Last Modified: 2010-05-03

I have this code;

Dim MyVal As Long

MyVall = MyVal + (b * (n And &HFF))       '2147481230 + 20202
MyVal = MyVal And &HFFFF

MyVal = 2147481230
b = 111
n = 307382

When it tries to add MyVal to b,n, it produces an overflow.  So I changed the data type to variant but then it had the overflow on the AND part of the code.

Any ideas how I can get around this?
many thanks
Question by:TJ_Eng
    1 Comment
    LVL 3

    Accepted Solution

    Hi TJ_Eng,

    You produce an overflow because Long is a signed value and the result of your equations gets larger than 0x7FFF FFFF (max. positive number).

    If you define MyVal as Variant VB automatically uses Variant / Double instead of overflowing which cannot be used together with logical operators (And).

    You could use another variable for saving the most significant unsigned bit of myval; perform your operation and logically add that bit again. This solution is valid if you only plan to perform hex operations (and, or, not, xor) afterwards (you get a negative decimal number):

    Dim Msb as long

    Msb = MyVal And &H40000000
    MyVal = MyVal and not &H40000000

    ' Perform your operations
    ' ...

    if (MyVal And &H40000000) And Msb then
        if (MyVal And &HC0000000) = &HC0000000 Then
            ' Error: still an overflow
            MyVal = MyVal Or &H80000000
        End If
        MyVal = MyVal or Msb
    end if

    Best regards,

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now