PDA

View Full Version : Effect of AND and OR operators on Type Long



stephglo
06-30-2011, 06:41 PM
Hello,
I am trying to decipher some rather interesting code written by someone else:
If (MyState AND (1 OR 32768)) = 0) then....
Where MyState is declared as a Long. The other two numbers are actually declared as constants of type Long as well, I just simplified for the sake of this post.
Can anyone tell me how AND and OR behave with type Long? I've previously seen AND used as a multiplication operator and OR as an addition operator but at least using some results in the Immediate window that doesn't appear to be what is going on here.
For example, the Immediate window gives (3 AND 34) = 2 which blows every theory I've come up with out of the water.
Ideas? I'm having a terrible time researching this since I can't find anyone addressing the use of typically Boolean operators with other types.
Thanks for any help, I truly appreciate it!

Paul_Hossler
06-30-2011, 07:02 PM
My guess is that there's bit mapping going on

34 = 00100010 in binary
3 = 00000011 in binary

so ANDing the bits = 00000010 in binary, or 2 in decimal

So

1 = 0000000000000001 in binary
32768 = 0100000000000000 in binary

so 1 OR 32768 would be

0100000000000001 in binary (if I did that right)

Paul

stephglo
06-30-2011, 07:56 PM
I like that theory. Still working out exactly what this code tests and why, but this is a definite start. Thank you!

GTO
06-30-2011, 08:28 PM
Hi Paul:hi:

I get:


1000000000000000
+ 0000000000000001
_____________________
1000000000000001

I think you were off by one column on the big number.

Stephglo,

You might be interested to read through http://www.vbaexpress.com/forum/showthread.php?t=32454 , where xld took pity on me with several nice answers (and Malcom turned me into a Democrat).

At post #4, you'll spot a couple of links that I found helpful as well. At the bottom of the thread you'll see where Paul gave examples where bitwise operations are common.

Hope that helps,

Mark

Bob Phillips
07-01-2011, 02:00 AM
It is definitely using bit comparisons as Paul says.

What it is testing is if myState is either 1 or 32768+n, where n=0,1,2,3,...

So 1 will pass, 32768 will pass, 32769 and any greater number will pass.

What I don't get is why it isn't just



If MyState AND 32769

or at least use a variable to load (1 OR 32768)

Bob Phillips
07-01-2011, 02:11 AM
This might help



Public Sub Test()
Const nOne As Long = 1
Const nOther As Long = 32768
Dim OrValue As Long
Dim myState As Long, i As Long
Dim addon As Long

OrValue = (nOne Or nOther)
For i = 1 To 100

addon = Application.RandBetween(-100, 1000)
myState = OrValue + addon
If i = 99 Then myState = 1
If i = 100 Then myState = 2
If myState And OrValue Then

Debug.Print myState & " passes"
Else

Debug.Print myState & " fails.................."
End If
Next i
End Sub

Paul_Hossler
07-01-2011, 05:43 AM
I think you were off by one column on the big number.



Score:

Mark - 1
Paul - 0



Paul

:friends: