vba masking bits not working

  • Thread starter Thread starter David F.
  • Start date Start date
D

David F.

dim key long

key=1251205

key=key And &HFFFF

why is key is still equal to 1251205 ???

Access 2003

TIA!!
 
I changed to use &HFFFF& and that seems to work.

Now a general question on overflows. How can I multiple two longs
65535*65535 and not the result but not get an overflow? I guess it
overflows because it's going negative, but I don't care, I'll be dumping the
high bits anyway.
 
Let me correct that..

Now a general question on overflows. How can I multiple two longs
65535*65535 and not lose the result, but not get an overflow?

Using on error resume just aborts the multiple.
 
I can get past the long multiplication overflow by using doubles, but now
how do I get it back to an integer.


Dim d

d=2717943004

d = d And 65535# ' << Fails with overflow error ?????
 
Okay - here's the answer unless someone has something better.

While d > 65535
d = d - &H10000
Wend
 
A four-byte variable whose value is FFFF has all the bits set, so
X And &HFFFF
will always equal X, so that's probably not what you want to do.

I believe that when you write
&HFFFF&
the appended & converts the four bytes FFFF to the two bytes FF (& is
the integer suffix, isn't it?) which are then coerced to the Long
00FF. So
X And &HFFFF&
is really
X And &H00FF
which may not be what you mean either.
 
VBA doesn't have an unsigned long or a quad, so there's no way of
multiplying FFFF by FFFF using traditional integer types. VBA has
partial support for scaled integer types Currency and Decimal: can you
use something like

Dim Product As Variant
Product = CDec(65535) * CDec(65535)

VarType(Product) returns 14, vbDecimal.
 
Back
Top