Solved

result of (X and Y)

Posted on 2002-07-17
6
162 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

821 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