Solved

# OverFlow problem

Posted on 2004-11-12
233 Views
Hi,

I have this code;

Dim MyVal As Long

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

where,
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
0
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

' ...

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

Best regards,
Maxim
0

## Featured Post

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) 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…