Discussion:
Problem with mask
(too old to reply)
Me
2006-11-23 18:03:02 UTC
Permalink
i have this little code

Private Sub Form_Load()
Dim i As Long, i2 As Long
i = 1193046 And &HFFFF
MsgBox Hex(i)
End Sub


now 1193046 in hex is 123456

but when i run the program, the value i get from msgbox is '123456'
which is incorrect since i did AND with FFFF.. so i should get '3456'

i think this is a problem with vb (big problem for me)
because if i do the AND using only &HFF it cuts it correctly..

http://www.phoenixbit.com
Me
2006-11-23 18:10:01 UTC
Permalink
i found a way to fix the problem...

Private Sub Form_Load()
Dim i As Long, i2 As Long
i = 1193046 And &H8000FFFF
i = i And &H7000FFFF
MsgBox Hex(i)
End Sub

the thing is you have to apply the mask.. but for the vb to get it
correctly you set the mask 8000FFF that will leave the
MSB ( most significant bit ) and the last 16 bits of the number
and then do the 70000FFFF mask to clear the MSB that may
have been left there from before (actually in case number was less than zero )

http://www.phoenixbit.com
Bob Butler
2006-11-23 18:25:21 UTC
Permalink
Post by Me
i found a way to fix the problem...
Private Sub Form_Load()
Dim i As Long, i2 As Long
i = 1193046 And &H8000FFFF
i = i And &H7000FFFF
MsgBox Hex(i)
End Sub
the thing is you have to apply the mask.. but for the vb to get it
correctly you set the mask 8000FFF that will leave the
MSB ( most significant bit ) and the last 16 bits of the number
and then do the 70000FFFF mask to clear the MSB that may
have been left there from before (actually in case number was less than zero )
Try
i = 1193046 And &HFFFF&

The constant &HFFFF fits into a 16-bit integer which VB then sign-extends to
32 bits so &HFFFF = &HFFFFFFFF. By appending a trailing & you tell VB that
the original constant is already 32 bits and it then uses &H0000FFFF which
is what you need.
--
Reply to the group so all can participate
VB.Net: "Fool me once..."
Me
2006-11-23 18:37:02 UTC
Permalink
Thanks man... works ok now

u saved me from a lot of work :P

http://www.phoenixbit.com

Loading...