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?
LVL 9
william007Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aikimarkCommented:
&HFFFF = -1 = &HFFFFFFFF

&HFFFFFFFF And &HF0000000 = &HF0000000

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
william007Author Commented:
Hi, aikimark,
&HFFFF=00000000000000001111111111111111
&HFFFFFFFF=11111111111111111111111111111111
Since like they are different, am I converted it wrongly?
0
william007Author Commented:
Since -->Seems
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

grg99Commented:
You have to be careful with sign-extension.  If you try to stuff FFFF into a 16-bit variable, it will look like -1.

If you then AND a 32-bit constant, such as 12345678, with the 16-bit variable, tthe variable will get sign-extended to 32 bits wide, and in 32 bits, minus one is FFFFFFFF.    Therefore you won't get what you expected.

0
aikimarkCommented:
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.
0
william007Author Commented:
Hi,

Can you elaborate more on VB's implementation?
0
william007Author Commented:
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)
0
william007Author Commented:
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...
0
aikimarkCommented:
try this
Public Function BitAndHex(parmHex1 As String, parmHex2 As String) As String
    Dim bH1(0 To 3) As Byte
    Dim bH2(0 To 3) As Byte
    Dim StringPosn As Long
    Dim BytePosn As Long
    Dim Temp As String
    BytePosn = UBound(bH1)
    Temp = Right$(parmHex1, 8)
    If (Len(Temp) Mod 2) <> 0 Then
        Temp = "0" & Temp   'prepend 0
    End If
    For StringPosn = Len(parmHex1) - 1 To 1 Step -2
        bH1(BytePosn) = "&h" & Mid$(parmHex1, StringPosn, 2)
        BytePosn = BytePosn - 1
    Next
    BytePosn = UBound(bH2)
    Temp = Right$(parmHex2, 8)
    If (Len(Temp) Mod 2) <> 0 Then
        Temp = "0" & Temp   'prepend 0
    End If
    For StringPosn = Len(parmHex2) - 1 To 1 Step -2
        bH2(BytePosn) = "&h" & Mid$(parmHex2, StringPosn, 2)
        BytePosn = BytePosn - 1
    Next
    Temp = String(8, "0")
    StringPosn = 1
    For BytePosn = LBound(bH2) To UBound(bH2)
        Mid$(Temp, StringPosn, 2) = Right$("0" & Hex(bH1(BytePosn) And bH2(BytePosn)), 2)
        StringPosn = StringPosn + 2
    Next
    BitAndHex = Temp
End Function

===============================
Private Sub Command1_Click()
   Text2 = bitandhex("FFFF","F0000000")
End Sub

'Text2  = "00000000"
0
aikimarkCommented:
If you put this into production, you should add a validation that the parameters are a valid hex string.
0
william007Author Commented:
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...
0
aikimarkCommented:
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

0
william007Author Commented:
Hi, is VB using Two’s Complement notation?
0
aikimarkCommented:
yes.  with signed integer values, there is only one zero representation.
0
KnobiKnobiCommented:
aikimark is right with "translation of constants"
add. info: (i´m not completly sure you are knowing)
&HFFFF& And &HF0000000 works cause a "&" at the and of a constant
is like saying &H0000FFFF And &HF0000000
The ending "&" forces constant to be assumed as 32 BIT Value
0
william007Author Commented:
>>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..


0
william007Author Commented:
>>we should postfix a &, eg &FFFF&.
Typo, is &HFFFF&.
0
aikimarkCommented:
<<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.
0
bchoorCommented:
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
0
bchoorCommented:
FYI,

also ?&HFFFF0000 = -65536
0
william007Author Commented:
>>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?
0
aikimarkCommented:
@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
0
william007Author Commented:
Thanks:-)
0
william007Author Commented:
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.
0
william007Author Commented:
Ok, finish regrading, thanks:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.