Seems there is a bug with long integers in VBA

G

Guest

Hi everybody,

I tried assign values using hexidecimal notation in Excel 2003 SP2
and found that &hFFFF is not equal to &h10000 - 1 for LONG integers.
Sample is here:
Sub test()
Const WorkaroundValue As Long = &H10000 - 1
Const WrongValue As Long = &HFFFF
Dim lVal As Long

lVal = &HFFFF

Debug.Print "lVal: " & lVal & " " & Hex(lVal)

If WorkaroundValue = WrongValue Then
Debug.Print "Equal"
Else
Debug.Print "Not Equal: " & WorkaroundValue & "," & WrongValue
Debug.Print "In hex: " & Hex(WorkaroundValue) & "," & Hex(WrongValue)
End If
End Sub

Cheers,
Alex
 
R

Ron Rosenfeld

Hi everybody,

I tried assign values using hexidecimal notation in Excel 2003 SP2
and found that &hFFFF is not equal to &h10000 - 1 for LONG integers.
Sample is here:
Sub test()
Const WorkaroundValue As Long = &H10000 - 1
Const WrongValue As Long = &HFFFF
Dim lVal As Long

lVal = &HFFFF

Debug.Print "lVal: " & lVal & " " & Hex(lVal)

If WorkaroundValue = WrongValue Then
Debug.Print "Equal"
Else
Debug.Print "Not Equal: " & WorkaroundValue & "," & WrongValue
Debug.Print "In hex: " & Hex(WorkaroundValue) & "," & Hex(WrongValue)
End If
End Sub

Cheers,
Alex

I believe this occurs because hex numbers with four or fewer digits are
converted to signed 2-byte integers. When the signed integer intermediate value
is converted to a Long (or 4-byte) integer, the sign is propagated.


--ron
 
G

Guest

&hFF processes correctly.

I just wonder why compiler doesn't allow to use &h00FFFF if it doesn't check
lvalue ?!

But I guess this question not for you. :)

Thank you.
 
P

Peter T

But I guess this question not for you. :)

Why that - Ron gave you a good concise answer !! I merely followed to show
how to set the Hex to a Long.

As Ron said, 4 digit Hex values are Integers unless explicitly set to a Long
with the trailing &

Integers range from -32768 to +32767, so -
&H8000 + &H7FFF = &HFFFF = -1

If you assign &HFFFF to a Long variable the sign is propagated and coerced
into the new Long value of -
Debug.Print Hex(CLng(&HFFFF)) ' &HFFFFFFFF

Regards,
Peter T


Alex said:
&hFF processes correctly.

I just wonder why compiler doesn't allow to use &h00FFFF if it doesn't check
lvalue ?!

But I guess this question not for you. :)

Thank you.

 
R

Ron Rosenfeld

&hFF processes correctly.

I just wonder why compiler doesn't allow to use &h00FFFF if it doesn't check
lvalue ?!

But I guess this question not for you. :)

Thank you.

Your initial theory was that there is a "bug with long integers in VBA"

As I explained to you, the problem is not that, but rather the problem is your
understanding of how VBA treats HEX numbers, and also with data conversions.

For example, a "minor" change in your routine prevents the hexadecimal values
from being treated as signed two-bit integers:

========================
Sub test()
Const WorkaroundValue As Long = &H10000 - 1
Const WrongValue As Long = &HFFFF&
Dim lVal As Long

lVal = &HFFFF&

Debug.Print "lVal: " & lVal & " " & Hex(lVal)

If WorkaroundValue = WrongValue Then
Debug.Print "Equal"
Else
Debug.Print "Not Equal: " & WorkaroundValue & "," & WrongValue
Debug.Print "In hex: " & Hex(WorkaroundValue) & "," & Hex(WrongValue)
End If
End Sub
=============================

The immediate window shows:

lVal: 65535 FFFF
Equal






--ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top