How Hex number works in if statements

rajkavikumar

New Member
Joined
Oct 2, 2012
Messages
20
Hello Friends I am trying to understand how I can use Hex in my if statement rather how below mtioned code is working in if condition.
Code:
Sub test1()

Const T = &H1
Const P = &H2
Const D = &H20
R = P + D
If R And P Then
    Debug.Print P
End If
If R And T Then
    Debug.Print T
End If


End Sub

I also checked in immediate window that
R AND P returns 2
and
R and T returns zero

but I don't understand that how this get calculated?:confused:
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi

This is a very basic question that means that you did not study binary/hexadecimal numbers.
You should search the web for arithmetic/logical operation with binary numbers, or bitwise operations. I'm sure you'll find lots of tutorials.

Anyway, for your case

T=P+D= 20H + 2H = 22H

To see the result of R and P, in Hexadecimal

22H
2H

since you are not used to it, convert the values to binary

00100010
00000010

Now it's easy to see that the only bit that is 1 in both numbers in the same place is the second one from the right, and so the result is

00000010

which converted to decimal is 2.

Hope this helps.
 
Upvote 0
P. S.

Also notice that vba only has 1 And operator which may be kind of confusing since in the worksheet the And() function assumes it's a boolean operation while the vba And operator considers an expression like yours a bitwise operation.
 
Upvote 0
Hey Thanks,
pgc01 I was not aware of that bit wise operations works in vba and that too using AND operator. :cool:
 
Upvote 0
Hey Thanks,
pgc01 I was not aware of that bit wise operations works in vba and that too using AND operator. :cool:

Yes, as I said it's a bit confusing if you are not used to it. For ex. in C you have 2 different operators, 1 for the bitwise And (&) and another for the boolean And (&&) and so there is no confusion.

We are not that lucky with vba. :(
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,823
Members
449,049
Latest member
cybersurfer5000

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top