ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Seems there is a bug with long integers in VBA (https://www.excelbanter.com/excel-programming/388971-seems-there-bug-long-integers-vba.html)

ALEX

Seems there is a bug with long integers in VBA
 
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 he
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

Ron Rosenfeld

Seems there is a bug with long integers in VBA
 
On Tue, 8 May 2007 09:52:02 -0700, Alex wrote:

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 he
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

ALEX

Seems there is a bug with long integers in VBA
 

&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.

"Ron Rosenfeld" wrote:

On Tue, 8 May 2007 09:52:02 -0700, Alex wrote:

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 he
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


Peter T

Seems there is a bug with long integers in VBA
 
Try this -

Debug.Print &HFFFF, &HFFFF& ' -1 65535

Regards,
Peter T

"Alex" wrote in message
...
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 he
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




ALEX

Seems there is a bug with long integers in VBA
 
Thank you!

"Peter T" wrote:

Try this -

Debug.Print &HFFFF, &HFFFF& ' -1 65535

Regards,
Peter T

"Alex" wrote in message
...
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 he
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





Peter T

Seems there is a bug with long integers in VBA
 
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" wrote in message
...

&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.

"Ron Rosenfeld" wrote:

On Tue, 8 May 2007 09:52:02 -0700, Alex

wrote:

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 he
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




Ron Rosenfeld

Seems there is a bug with long integers in VBA
 
On Tue, 8 May 2007 10:41:01 -0700, Alex wrote:

&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


All times are GMT +1. The time now is 06:43 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com