This is because the numbers 10 and 1000 have been converted to binary representation:
1010
1111101000
The AND operation checks each corresponding bit at a time, and where the bit in the top and the
bottom row is 1, the answer is 1:
0000001000
. ..which when converted back to decimal notation is our result of 8. All this means that the logical
operator AND is used to detect when two conditions are present simultaneously. Here’s a self
explanatory application:
10 CLS:INPUT "The number of the day";day
20 INPUT "The number of the month";month
30 IF day=25 AND month=12 GOTO 50
40 GOTO 10
50 PRINT "Merry Christmas!"
OR works on bits as well, where the result is 1 unless both bits from the arguments are 0, in which
case the result is 0. Using the same numbers as for the AND example
PRINT 1000 OR 10
1002
Bitwise:
1010
1111101000
Resulting in the answer:
1111101010
And in the program example:
10 CLS:
20 INPUT "The number of month";month
30 IF month=12 OR month=1 OR month=2 GOTO 50
40 CLS:GOTO 10
50 PRINT "It must be winter"
10 CLS
20 INPUT "The number of the month";month
30 IF NOT(month=6 OR month=7 OR OR month=8) GOTO 50
40 CLS:GOTO 10
50 PRINT "It can't be summer!"
The final major feature to consider is the fact that you can add together a number of logical
conditions to distill the facts yet further (up to the maximum line length) in fact:
10 CLS:INPUT "The number of the day";day
20 INPUT "The number of the month";month
30 IF NOT(month=12 OR month=1) AND day=29 GOTO 50
40 CLS:GOTO 10
50 PRINT "This is neither December nor
January,but this might be a leap year"
The result of a relational expression is either -1 or 0. The bit representation for -1 is all bits of the
integer = 1; for 0 all bits of the integer are 0. The result of a logical operation on two such arguments
will yield either -1 for True, or 0 for False.
Check this by adding line 60 to the above program:
60 PRINT NOT(month=12 OR month=1)