FutureBasic Logo

<<    Index    >> FutureBasic 5

and   operator

result = exprA {and | &&} exprB

Expression exprA and expression exprB are each interpreted as 32-bit integer quantities. The and operator performs a bitwise comparison of each bit in exprA with the bit in the corresponding position in exprB . The result is another 32-bit quantity; each bit in the result is determined as follows:
Bit value in
Bit value in
Bit value in
0 0
1 0 0
0 1 0
1 1 1

In the following example, expressions are evaluated as true or false before a decision is made for branching. The logical expression time>7 is true, and is therefore evaluated as -1. The expression time<8.5 is false, and is therefore evaluated as 0. Then the bitwise comparison (-1) and (0) is performed, resulting in zero. Finally, the long if statement interprets this zero result as meaning "false," and therefore skips the first print statement.
time = 9.5
long if ( time > 7 and time < 8.5 )
   print "It is time for breakfast!"
   print "We have to wait 'til noon to eat!"
end if

The example below shows how bits are manipulated with and:
defstr long
print bin$(123)
print "--------------------------------"
print bin$(923 and 123)

program output:

In a statement like if expr1 and expr2 then..., it is possible for " expr1 and expr2 " to be false even though each individual expr is evaluated as true. Consider this example:
JoeIsHere = 16
FredIsHere = 2
if JoeIsHere then print "Joe's here" else print "Joe's gone"
if FredIsHere then print "Fred's here"¬
  else print "Fred's gone"
long if JoeIsHere and FredIsHere
  print "They're both here"
  print "They're not both here!"
end if

program output:
Joe's here
Fred's here
They're not both here!

This strange result happens because the expression "16 and 2" evaluates to 0, which is then interpreted as "false" by the long if statement. This wouldn't have happened if we had set JoeIsHere to -1 and FredIsHere to -1, because the expression "-1 and -1" evaluates to -1.

See Also:
nand; nor; not; xor; or; Appendix D - Numeric Expressions