Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple IF statement 1

Status
Not open for further replies.

Andrzejek

Programmer
Jan 10, 2006
8,502
US
Let's start simple:

Code:
If True Then
    MsgBox True
Else
    MsgBox False
End If
Gives True

Code:
If 1 Then
    MsgBox True
Else
    MsgBox False
End If
Gives True

Code:
If 8 Then
    MsgBox True
Else
    MsgBox False
End If
Gives True, so now

Code:
If 1 And 8 Then
    MsgBox True
Else
    MsgBox False
End If
Gives False :-(, why?

but:
Code:
If 2 And 7 Then
    MsgBox True
Else
    MsgBox False
End If
is True - weird!

Had enough? Try this:
Code:
If CBool(1) And CBool(8) Then
    Debug.Print CBool(1)
    Debug.Print CBool(8)
    MsgBox True
Else
    Debug.Print CBool(1)
    Debug.Print CBool(8)
    MsgBox False
End If

Anybody cares to explain... [ponder]


---- Andy

There is a great need for a sarcasm font.
 
1 And 8 seems to be bitwise AND, i.e.:
1 is in binary 0001
8 is in binary 1000
then 1 AND 8 = 0001 AND 1000 = 0000 = FALSE
 
For what it's worth, using VBA in Excel.

Code:
Public Sub TestBool()

For count = 0 To 9
    Debug.Print ("Cbool(" & count & ") returns " & CBool(count) & "; but " & count & " = True evaluates as " & (count = True))
Next count
End Sub
Returns
Cbool(0) returns False; but 0 = True evaluates as False
Cbool(1) returns True; but 1 = True evaluates as False
Cbool(2) returns True; but 2 = True evaluates as False
Cbool(3) returns True; but 3 = True evaluates as False
Cbool(4) returns True; but 4 = True evaluates as False
Cbool(5) returns True; but 5 = True evaluates as False
Cbool(6) returns True; but 6 = True evaluates as False
Cbool(7) returns True; but 7 = True evaluates as False
Cbool(8) returns True; but 8 = True evaluates as False
Cbool(9) returns True; but 9 = True evaluates as False

 
>1 And 8 seems to be bitwise AND

Quite so - and this is documented

Microsoft VBA documentation said:
result = expression1 And expression2
The And operator [] performs a bitwise comparison of identically positioned bits in two numeric expressions

(There are other oddities caused by VBA's expression evaluator's implicit type coercion, and the definition of the Boolean constants)

 

mikrom said:
then 1 AND 8 = 0001 AND 1000 = 0000 = FALSE

I think I've got it.
[pre]
1 - [blue]0001 [/blue]
8 - [green]1000[/green]
[/pre]
So every bitwise 'digit' from 1 is compared (multiplied) by corresponding bitwise 'digit' from 8, i.e.[pre]
[blue]0[/blue] x [green]1[/green] = 0
[blue]0[/blue] x [green]0[/green] = 0
[blue]0[/blue] x [green]0[/green] = 0
[blue]1[/blue] x [green]0[/green] = 0[/pre]
and if all outcomes are 0, we have False. And if at least one outcome is 1, we get True.

Did I get it [ponder] (kind of... sort of... Good enough?)



---- Andy

There is a great need for a sarcasm font.
 
Correction - if the outcome is 0, it is false, non-zero it is true.
 
What are you correcting here? Andy was talking about bitwise operations, and so was actually saying the same as you - if just one bit of the result is set (i.e numerical result is non-zero) then we get True. Don't think he was suggesting that (only) 1 = True
 
The bitwise logic is used in swap algorithm of integers, without third variable:
[pre]Dim a As Integer, b As Integer
a = 123: b = 345
MsgBox "before swap: a=" & a & " ,b=" & b
a = a Xor b
b = b Xor a
a = a Xor b
MsgBox "after swap: a=" & a & " ,b=" & b[/pre]


combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top