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

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,887
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.
 

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,887
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.
 

rajkavikumar

New Member
Joined
Oct 2, 2012
Messages
20
Hey Thanks,
pgc01 I was not aware of that bit wise operations works in vba and that too using AND operator. :cool:
 

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,887
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. :(
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,487
Messages
5,831,987
Members
430,100
Latest member
namhnz

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
Top