&HFFFFFFFF And &HF0000000 = &HF0000000

Solved

Posted on 2006-03-21

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?

Text2 = "&H" & Hex(&HFFFF And &HF0000000)

End Sub

I get the value &HF0000000 from the above function

But

&HFFFF=0000000000000000111

&HF0000000=111100000000000

Isn't is supposed to be &H0, as

11110000000000000000000000

00000000000000001111111111

--------------------------

00000000000000000000000000

What is the problem?

25 Comments

&HFFFF=0000000000000000111

&HFFFFFFFF=111111111111111

Since like they are different, am I converted it wrongly?

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.

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

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

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"

However, I wish to understand more about how VB operate the AND operation, as it is important to us...

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

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

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

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.

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

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?

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

Title | # Comments | Views | Activity |
---|---|---|---|

sort a text field that contains numbers( query | 9 | 47 | |

How to inactivate a tab on a form based on permissions? | 11 | 56 | |

Problem to copy file | 7 | 43 | |

JSON Response and request in VB6 application | 11 | 55 |

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

Connect with top rated Experts

**18** Experts available now in Live!