Link to home
Start Free TrialLog in
Avatar of william007
william007

asked on

Bitwise question for &HFFFF And &HF0000000

Private Sub Command1_Click()
Text2 = "&H" & Hex(&HFFFF And &HF0000000)
End Sub


I get the value &HF0000000 from the above function

But
&HFFFF=00000000000000001111111111111111
&HF0000000=11110000000000000000000000000000

Isn't is supposed to be &H0, as

11110000000000000000000000000000
00000000000000001111111111111111
------------------------------------------------
00000000000000000000000000000000

What is the problem?
ASKER CERTIFIED SOLUTION
Avatar of aikimark
aikimark
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of william007
william007

ASKER

Hi, aikimark,
&HFFFF=00000000000000001111111111111111
&HFFFFFFFF=11111111111111111111111111111111
Since like they are different, am I converted it wrongly?
Since -->Seems
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
it isn't a matter of how you convert it as much as how VB implements it.

You are (mathematically/CompSci) correct in your understanding of bit-wise AND operations on hexadecimal numbers, if you break away from VB's implementation.
Hi,

Can you elaborate more on VB's implementation?
Hi, grg99,
Long datatype in VB is 4 bytes,
I do something like this, still get the same result.
Dim a As Long
Dim b As Long
a = &HFFFF
b = &HF0000000
Text2 = "&H" & Hex(a And b)
Dim a As Long
Dim b As Long
a = &HFFFF& <---at at postfix & here
b = &HF0000000
Text2 = "&H" & Hex(a And b)

Only I use the code above, I get &H0...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you put this into production, you should add a validation that the parameters are a valid hex string.
Thanks for your code aikimark, and it is very useful:)
However, I wish to understand more about how VB operate the AND operation, as it is important to us...
the bit-wise AND operation performs exactly as you understand it.  Your VB results are a result of how the VB compiler translates hexadecimal literals.  VB treats 1 to 3 hex digits in a hex literal as an unsigned number (0 -> 4095).  

VB treats 4 to 8 hex digits as a signed integer.  The sign of the number is determined by the high-order bit in the first non-zero hex digit.  If the high-order bit = 1, the number is negative.

type the following in your immediate window:
?&h0fff, &hffff

you should see the folloiwing results:
 4095         -1

Hi, is VB using Two’s Complement notation?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>The sign of the number is determined by the high-order bit in the first non-zero hex digit
But &FFFFF gives us 1048575(+ve)
--------
Can I conclude that
If we specify (1-4 Hex digits)
&HF
&HFF
&HFFF
&HFFFF
VB will treat them as 16 bit value, and store their result value (In decimal) in the variable.
If we want VB to treat it as 32 bit value, we should postfix a &, eg &FFFF&.

If we specify (5-8 Hex digits)
&HFFFFF
&HFFFFFF
&HFFFFFFF
&HFFFFFFFF
VB will auto treat it as 32 bit value(As &HFFFFF can never be a 16 bit value), and store their result value (In decimal) in the variable.

Is my conclusion correct? If any one find any pitfall in this conclusion, please pointed out..


>>we should postfix a &, eg &FFFF&.
Typo, is &HFFFF&.
<<But &FFFFF gives us 1048575(+ve)>>

My comment about the calculation of the sign was wrong.  Sorry about that.  It is based on the translation of the hex digits within a signed (small/long) integer numeric representation.
Here's a test I ran in my Immediate Window to illustrate this.  When the number of digits reaches the limit for the (small/long) integer datatype, the high-order bit determines the sign.
?&H8, &HF
 8             15
?&H80, &HF0
 128           240
?&H800, &HF00
 2048          3840
?&H8000, &HF000
-32768        -4096
?&H80000, &HF0000
 524288        983040
?&H800000, &HF00000
 8388608       15728640
?&H8000000, &HF000000
 134217728     251658240
?&H80000000, &HF0000000
-2147483648   -268435456

=================================
<<&HFFFF&>>
It shouldn't matter.  VB will automatically increase the size (recast) of the smaller number to align with the larger one, especially for bit-wise operations.
Ok this is what I tried and it worked

Public Sub Test()
    Dim n1 As String
    Dim n2 As String
   
    n1 = "&HFFFF"
    n2 = "&HF0000000"
   
    Debug.Print Int(n1) And Int(n2)

   
End Sub

When assigning it a string value - it seems to work

&HFFFF (also &H0000FFFF) = 65535, for some reason, VB is giving -1 which is not correct. I tested in VB.NET 2005 and it all works correctly in there


HTH
~BC
FYI,

also ?&HFFFF0000 = -65536
>>It shouldn't matter.  VB will automatically increase the size (recast) of the smaller number to align with the larger one, especially for bit-wise operations.
However, the test case below shows that...
Dim a As Long
Dim b As Long
a = &HFFFF
b = &HF0000000
Text2 = "&H" & Hex(a And b)
>>Result &HF0000000

Dim a As Long
Dim b As Long
a = &HFFFF& <---at at postfix & here
b = &HF0000000
Text2 = "&H" & Hex(a And b)
>>Result &H0

Btw, Can anybody look at my conclusion whether it is correct in term of VB 6.0? If not can anyone give an EXAMPLE to prove that it is not correct?
@william007

Take a close look at what you are doing and you will likely answer you own question.  I will help you by drawing your attention to the code used in your original question post:
Text2 = "&H" & Hex(&HFFFF And &HF0000000)

and what you most recently posted:
Dim a As Long
Dim b As Long
a = &HFFFF& <---at at postfix & here
b = &HF0000000
Text2 = "&H" & Hex(a And b)

=====================
What's different?
Why would the results differ?
Doesn't this illustrate what we experts have been posting about VB recasting variables?

=====================
Rather than use the trailing ampersand, I'd recommend using the CLng() function as it's purpose is MUCH more evident.
Example:
?CLng("&H" & "FFFF")
 65535

?CLng("&H" & "0000FFFF")
 65535
Thanks:-)
Sorry, I wish accepted the first comment as the answer, but the third comments appear as accepted.
I will request for reopen this question to grade it again.
Ok, finish regrading, thanks:)