Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# result of (X and Y)

Posted on 2002-07-17
Medium Priority
169 Views
I am working on a maintenace system which has code which I do not understand,

basically it has:

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()
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
Question by:damienm
• 3
• 2

LVL 4

Expert Comment

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 143

Expert Comment

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

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

LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 200 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

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

Thanks

Damien
0

LVL 143

Expert Comment

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

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
###### Suggested Courses
Course of the Month5 days, 20 hours left to enroll