Home > Archive > Visual Basic > August 2005 > Ampersands (&) in Hexadecimal numbers, What's the rule?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Ampersands (&) in Hexadecimal numbers, What's the rule?
|
|
| Hong Kong Phooey 2005-08-30, 6:55 pm |
| I feel dumb asking this, haveing programmed in VB for so long, but here
goes...
What is the rule for putting an ampersand (&) at the END of a hexadecimal
number in VB?
For example:
Dim x As Integer
Dim y As Integer
Dim z As Integer
x = &H8000
y = &H8000& 'Produces a different value
z = &H00008000 'VB Truncates leading zeroes (equal to x)
I'm just trying to understand the behavior.
I don't come across much usage for this syntax, so I'd just like to know for
the next time I encounter it.
| |
| Charles Law 2005-08-30, 6:55 pm |
| Hi Hong Kong
Putting an ampersand at the end (when you assign to y) makes the value
&H8000 double-precision. The trailing ampersand in this case has nothing to
do with hexadecimal notation.
HTH
Charles
"Hong Kong Phooey" <NOSPAM@NOSPAM.com> wrote in message
news:%23%23IhHxarFHA.3836@TK2MSFTNGP12.phx.gbl...
>I feel dumb asking this, haveing programmed in VB for so long, but here
> goes...
>
> What is the rule for putting an ampersand (&) at the END of a hexadecimal
> number in VB?
> For example:
>
> Dim x As Integer
> Dim y As Integer
> Dim z As Integer
> x = &H8000
> y = &H8000& 'Produces a different value
> z = &H00008000 'VB Truncates leading zeroes (equal to x)
>
> I'm just trying to understand the behavior.
> I don't come across much usage for this syntax, so I'd just like to know
> for
> the next time I encounter it.
>
>
| |
| Karl E. Peterson 2005-08-30, 6:55 pm |
| Hong Kong Phooey wrote:
> I feel dumb asking this, haveing programmed in VB for so long, but
> here goes...
>
> What is the rule for putting an ampersand (&) at the END of a
> hexadecimal number in VB?
> For example:
>
> Dim x As Integer
> Dim y As Integer
> Dim z As Integer
> x = &H8000
> y = &H8000& 'Produces a different value
> z = &H00008000 'VB Truncates leading zeroes (equal to x)
>
> I'm just trying to understand the behavior.
> I don't come across much usage for this syntax, so I'd just like to
> know for the next time I encounter it.
The trailing ampersand tells VB it's a Long value (4 byte), even for values that
could be held in an Integer (2 byte). Basically, if it's &h8000-&hFFFF, use the
ampersand (actually, it's never a bad idea), assuming you really do want to store it
in a 4-byte variable. Otherwise, VB strives to confuse, by twiddling the sign bit
for those higher values.
--
Working Without a .NET?
http://classicvb.org/petition
| |
| Charles Law 2005-08-30, 6:55 pm |
| Correction:
I mean long, not double-precision
Charles
"Charles Law" <blank@nowhere.com> wrote in message
news:eP$V60arFHA.3096@TK2MSFTNGP15.phx.gbl...
> Hi Hong Kong
>
> Putting an ampersand at the end (when you assign to y) makes the value
> &H8000 double-precision. The trailing ampersand in this case has nothing
> to do with hexadecimal notation.
>
> HTH
>
> Charles
>
>
> "Hong Kong Phooey" <NOSPAM@NOSPAM.com> wrote in message
> news:%23%23IhHxarFHA.3836@TK2MSFTNGP12.phx.gbl...
>
>
| |
| Herfried K. Wagner [MVP] 2005-08-30, 9:55 pm |
| "Hong Kong Phooey" <NOSPAM@NOSPAM.com> schrieb:
> What is the rule for putting an ampersand (&) at the END of a hexadecimal
> number in VB?
> For example:
>
> Dim x As Integer
> Dim y As Integer
> Dim z As Integer
> x = &H8000
> y = &H8000& 'Produces a different value
For VB.NET, see:
Visual Basic Language Specification -- 2.2.1 Type Characters
<URL:http://msdn.microsoft.com/library/e...VBSpec2_2_1.asp>
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
| |
|
|
|
| Just to amplify on Karl's comment, "Otherwise, VB strives to confuse, by
twiddling the sign bit for those higher values.", in case this warning
wasn't strong enough.
Always include the trailing "&" ampersand on all values you intend to use
for bit-map (flags, masks, etc) comparisons with other bit/hex defines,
otherwise results can be rather surprising.
-ralph
|
|
|
|
|