logical and in vb ?

S

Sam

Hi,
I'm trying to do the following :

If I have a value of 7 then I want to test if all the values from 0 to
4 can be contained in 7:
2^0 = 1 < 7 so can be contained in 7
2^4 = 16 > 7 so can't be contained in 7

I should do something like 7&0 < 0 ? but I don't know the vb
syntax....
Can someone help?
thx

Sam
 
S

Stephany Young

If (x And 7) = x ' x can be contained in 7

If (x And 7) <> x ' x cannot be contained in 7
 
S

Sam

humm actually i'm not sure it's what i want.
it returns x all the time as long as x is < 7. But 6 (for example)
cannot be contained in 7 : 2^6 = 64 > 7

Am I wrong ?
 
S

Stephany Young

If you are talking about using a value as a 'field' to hold 'flags' e.g.:

0
1
2
4
8
16
32
64
etc.

Then it will still work. You need to think about the bits.

111 = 7 = 4 Or 2 Or 1
110 = 6 = 4 Or 2
101 = 5 = 4 Or 1
100 = 4 = 4
011 = 3 = 2 Or 1
010 = 2 = 2
001 = 1 = 1
000 = 0 = 0

So, given:

v = 7
a = 0
b = 1
c = 2
d = 4

Then:

v = a Or b Or c Or d

If (v And a) = a Then
Debug.Print "v contains a"
Else
Debug.Print "v does not contain a"
End If

If (v And b) = b Then
Debug.Print "v contains b"
Else
Debug.Print "v does not contain b"
End If

If (v And c) = c Then
Debug.Print "v contains c"
Else
Debug.Print "v does not contain c"
End If

If (v And d) = d Then
Debug.Print "v contains d"
Else
Debug.Print "v does not contain d"
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top