FutureBasic Logo

<<    Index    >> FutureBasic 5

and   operator



Syntax:
result = exprA {and | &&} exprB

Description:
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
exprA
Bit value in
exprB
Bit value in
result&
0
0 0
1 0 0
0 1 0
1 1 1

Example:
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!"
xelse
   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$(923)
print bin$(123)
print "--------------------------------"
print bin$(923 and 123)

program output:
00000000000000000000001110011011
00000000000000000000000001111011
--------------------------------
00000000000000000000000000011011


Note:
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"
xelse
  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