We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Bitwise question for &HFFFF And &HF0000000

on
Medium Priority
1,059 Views
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?
Comment
Watch Question

View Solutions Only

CERTIFIED EXPERT
Top Expert 2014
Commented:
&HFFFF = -1 = &HFFFFFFFF

&HFFFFFFFF And &HF0000000 = &HF0000000

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
Hi, aikimark,
&HFFFF=00000000000000001111111111111111
&HFFFFFFFF=11111111111111111111111111111111
Since like they are different, am I converted it wrongly?

Commented:
Since -->Seems
Commented:
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.

CERTIFIED EXPERT
Top Expert 2014

Commented:
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.

Commented:
Hi,

Can you elaborate more on VB's implementation?

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)

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...
CERTIFIED EXPERT
Top Expert 2014
Commented:
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"
CERTIFIED EXPERT
Top Expert 2014

Commented:
If you put this into production, you should add a validation that the parameters are a valid hex string.

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...
CERTIFIED EXPERT
Top Expert 2014

Commented:
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

Commented:
Hi, is VB using Two’s Complement notation?
CERTIFIED EXPERT
Top Expert 2014
Commented:
yes.  with signed integer values, there is only one zero representation.
Commented:
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

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..

Commented:
>>we should postfix a &, eg &FFFF&.
Typo, is &HFFFF&.
CERTIFIED EXPERT
Top Expert 2014

Commented:
<<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.

Commented:
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

Commented:
FYI,

also ?&HFFFF0000 = -65536

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?
CERTIFIED EXPERT
Top Expert 2014

Commented:
@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

Commented:
Thanks:-)

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.

Commented:
Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile