# Hexadecimal Series



## xenou (Apr 21, 2011)

Given this series:


```
Sub Foo()
Dim a, i As Long

    a = Array(&H1, &H2, &H4, &H8, &H10, &H20, &H40, &H80, &H100, &H200, &H400, _
        &H800, &H1000, &H2000, &H4000, &H8000, &H10000, &H20000, _
        &H40000, &H80000, &H100000, &H200000)
    For i = 0 To UBound(a)
        Debug.Print a(i)
    Next i
    
End Sub
```

Output is:
 1 
 2 
 4 
 8 
 16 
 32 
 64 
 128 
 256 
 512 
 1024 
 2048 
 4096 
 8192 
 16384 
-32768 
 65536 
 131072 
 262144 
 524288 
 1048576 
 2097152

So...why is &H8000 the one and only negative number (when converted to decimal)?

Cheers,
ξ


----------



## Jaafar Tribak (Apr 21, 2011)

I think it is because the array being a variant it dynamically converts its elements from *0* to *15* into signed Integers and into signed Longs afterwards .This means that element 15 coincides with the bit *16* (Sign bit) of the &H8000 integer which is being set hence giving you the negative number.

To coerce the 15 element of the array into a Long you could just add an ampersand at the end of the Hex value like : &H8000*&*


----------



## xenou (Apr 21, 2011)

Thanks Jaafar.  

I'm not sure I get it but I'll keep chewing on this.  
I had thought it might be about integers and longs and had only tried the (futile):
_Debug.Print CLng(a(i))
_

I guess you could say I was ♪ looking for Long in all the wrong places ♫ ...


----------



## pgc01 (Apr 23, 2011)

xenou said:


> I'm not sure I get it but I'll keep chewing on this.


 
Hi xenou

Like Jaafar says until 16 bits the hexa is coerced into an Integer and thereafter to a Long. Try:


```
MsgBox _
    "H2000: " & TypeName(&H2000) & vbNewLine & _
    "H4000: " & TypeName(&H4000) & vbNewLine & _
    "H8000: " & TypeName(&H8000) & vbNewLine & _
    "H10000: " & TypeName(&H10000) & vbNewLine & _
    "H20000: " & TypeName(&H20000)
```


----------



## pgc01 (Apr 23, 2011)

... and this, of course, means you'll have the same behaviour some bits further on:


```
Debug.Print &H80000000
```


----------



## xenou (Apr 23, 2011)

I see.  Good examples.
I noticed this when adapting some API code for a file chooser (and got interesting in the AND'ing and OR'ing of the flags):


```
Global Const ahtOFN_FILEMUSTEXIST = &H1000
Global Const ahtOFN_CREATEPROMPT = &H2000
Global Const ahtOFN_SHAREAWARE = &H4000
Global Const ahtOFN_NOREADONLYRETURN = &H8000
```

So the bytes are correct but it's just that the immediate window displays it interpreting the first bit as a sign holder ...


----------

