Predict the output - Part II

How many did you get right?


  • Total voters
    7

Oorang

Well-known Member
Joined
Mar 4, 2005
Messages
2,071
Code:
Option Explicit

Const i = 9000
Const j = &H9000
Const k = &H9000&
Const l As Integer = 9000
Const m As Integer = &H9000
'Const n As Integer = &H9000&
Const o As Long = 9000
Const p As Long = &H9000
Const q As Long = &H9000&


Sub Example()
    MsgBox Join(Array(i, j, k, l, m, o, p, q), vbNewLine)
End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Explanation:
Literals have types too. The VBE automatically assigns them in most cases. The default number type is Integer. The VB IDE will then assign according to this pattern: If the number is larger than a Integer it becomes a long. And if it is larger than a Long it becomes a Double.

Ok so meanwhile, the &H prefix does not return an Signed Integer (the normal VB type) it returns an "Unsigned Integer". An unisgned integer (2 bytes) is one bit for the sign (positive or negative) and the remaining 15 bits for the number. Which means it can hold positive or negative numbers from 0 to 2^15. A unsigned integer does not reserve the 16th bit for a sign and so can hold 2^16 numbers.

Ok so what happens?
The VB IDE looks at any number less than 2^16 and goes "Hey that's an integer". But it doesn't realize that last bit is not a sign, it holds a value. Well that's not a problem for the range of numbers less than 2^15 because those bits mean the same thing for signed and unsigned integers. You run into trouble for just the range of numbers that are 16 bits wide and use the 16th bit for value. Because the VB IDE will go "Oh hey" that means the number is negative. Then other 15 bits are inverted (because they count down) and your number gets really wacky. If the number is more than 16 bits the VB IDE goes "OH hey that's a long" and the 16 bit of a Signed Long is a value bit so it reads correctly.
Ok so how to force the VB IDE to read a 16 Bit number as long? Well you just recast it using a Type Declaration Character (TDC). When a literal has a TDC, the VB IDE won't try to interpret the type, it will use the type you told it to. Problem solved... Until you overflow the long type at FFFFFFFF :)






http://support.microsoft.com/kb/38888
http://support.microsoft.com/kb/38888
 

Forum statistics

Threads
1,222,647
Messages
6,167,331
Members
452,110
Latest member
eui

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top