Solved

result of (X and Y)

Posted on 2002-07-17
6
161 Views
Last Modified: 2010-05-02
I am working on a maintenace system which has code which I do not understand,

basically it has:

Public Sub Load(lngOptions As Long)

If (lngOptions And 1) = 0 then
     'Do something
     MsgBox "Got here 1"
End If
If (lngOptions And 2) = 0 then
     'Do something else
     MsgBox "Got here 2"
End If
If (lngOptions And 4) = 0 then
     'Do something
     MsgBox "Got here 4"
End If
If (lngOptions And 32) = 0 then
     'Do something else
     MsgBox "Got here 32"
End If

End Sub

(I have added the message boxes)

So if I have,

Private Sub Command1_Click()
Call Load(1)
End Sub

All but the first msgbox come up but if I have Call Load(10) all but the second 1 will come up.  So basically I am looking for some sort of general Algorithm to determine what the result of (X And Y) where X and Y are Integers.

Thanks

Damien
0
Comment
Question by:damienm
  • 3
  • 2
6 Comments
 
LVL 4

Expert Comment

by:gencross
ID: 7159243
Do you want to add X and Y together and return the result?  Where are the values of X and Y in the code?

Please give details on exactly what you want to accomplish.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7159251
the X AND Y returns the bitwise result, so you have to "see" the integers in their bit representation:

0 -> 00000000
1 -> 00000001
2 -> 00000010
3 -> 00000011
4 -> 00000100
5 -> 00000101
6 -> 00000110
7 -> 00000111
etc
10-> 00001010

10 AND  1 -> 1010 AND 00001 -> 0
10 AND  2 -> 1010 AND 00010 -> 1
10 AND  4 -> 1010 AND 00100 -> 0
10 AND 32 -> 1010 AND 10000 -> 0

CHeers



0
 
LVL 2

Author Comment

by:damienm
ID: 7159254
I want to know if I have e.g.

msgbox (1 And 2)

or

msgbox (4 And 2)

What will be returned without me having to run the code.

So a general algorithm

for the result of
(X And Y)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 total points
ID: 7159290
in fact, my last output was incomplete:

10 AND  1 -> 01010 AND 00001 -> 00000 -> 0
10 AND  2 -> 01010 AND 00010 -> 00010 -> 2
10 AND  4 -> 01010 AND 00100 -> 00000 -> 0
10 AND 32 -> 01010 AND 10000 -> 00000 -> 0

bitwise AND returns a bit set to 1 only if that bit for both input was set to 1, otherwise the result is 0 for that bit.

CHeers
0
 
LVL 2

Author Comment

by:damienm
ID: 7159304
That explains the result although it does not explain why anyone would want to do this.

Thanks

Damien
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7159308
the explanation is simply that instead of passing x boolean arguments, you pass 1. The advantage is that if you add another IF( ), you don't need to change the procedure arguments, which can be important for versioning (compatibility).
CHeers
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

863 members asked questions and received personalized solutions in the past 7 days.

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

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now