?
Solved

Bitwise question for &HFFFF And &HF0000000

Posted on 2006-03-21
26
Medium Priority
?
919 Views
Last Modified: 2008-01-09
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?
0
Comment
Question by:william007
  • 13
  • 8
  • 2
  • +2
25 Comments
 
LVL 46

Accepted Solution

by:
aikimark earned 1200 total points
ID: 16250141
&HFFFF = -1 = &HFFFFFFFF

&HFFFFFFFF And &HF0000000 = &HF0000000

0
 
LVL 9

Author Comment

by:william007
ID: 16250190
Hi, aikimark,
&HFFFF=00000000000000001111111111111111
&HFFFFFFFF=11111111111111111111111111111111
Since like they are different, am I converted it wrongly?
0
 
LVL 9

Author Comment

by:william007
ID: 16250193
Since -->Seems
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 22

Assisted Solution

by:grg99
grg99 earned 400 total points
ID: 16250289
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
 
LVL 46

Expert Comment

by:aikimark
ID: 16250311
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
 
LVL 9

Author Comment

by:william007
ID: 16250358
Hi,

Can you elaborate more on VB's implementation?
0
 
LVL 9

Author Comment

by:william007
ID: 16250382
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
 
LVL 9

Author Comment

by:william007
ID: 16250488
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
 
LVL 46

Assisted Solution

by:aikimark
aikimark earned 1200 total points
ID: 16250572
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
 
LVL 46

Expert Comment

by:aikimark
ID: 16250588
If you put this into production, you should add a validation that the parameters are a valid hex string.
0
 
LVL 9

Author Comment

by:william007
ID: 16251625
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
 
LVL 46

Expert Comment

by:aikimark
ID: 16251921
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
 
LVL 9

Author Comment

by:william007
ID: 16252391
Hi, is VB using Two’s Complement notation?
0
 
LVL 46

Assisted Solution

by:aikimark
aikimark earned 1200 total points
ID: 16252434
yes.  with signed integer values, there is only one zero representation.
0
 
LVL 2

Assisted Solution

by:KnobiKnobi
KnobiKnobi earned 400 total points
ID: 16254831
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
 
LVL 9

Author Comment

by:william007
ID: 16256080
>>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
 
LVL 9

Author Comment

by:william007
ID: 16256117
>>we should postfix a &, eg &FFFF&.
Typo, is &HFFFF&.
0
 
LVL 46

Expert Comment

by:aikimark
ID: 16256857
<<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
 
LVL 10

Expert Comment

by:bchoor
ID: 16260367
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
 
LVL 10

Expert Comment

by:bchoor
ID: 16260382
FYI,

also ?&HFFFF0000 = -65536
0
 
LVL 9

Author Comment

by:william007
ID: 16260523
>>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
 
LVL 46

Expert Comment

by:aikimark
ID: 16261158
@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
 
LVL 9

Author Comment

by:william007
ID: 16267887
Thanks:-)
0
 
LVL 9

Author Comment

by:william007
ID: 16267896
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
 
LVL 9

Author Comment

by:william007
ID: 16268726
Ok, finish regrading, thanks:)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question