Important Notice
The pages on this site contain documentation for very old MS-DOS software,
purely for historical purposes.
If you're looking for up-to-date documentation, particularly for programming,
you should not rely on the information found here, as it will be woefully
out of date.
Example
◄Logical Operators► ◄Contents► ◄Index► ◄Back►
──────────────────────────────────────────────────────────────────────────────
The following example prints a "truth table":
CLS
PRINT " X Y NOT AND OR ";
PRINT "XOR EQV IMP"
PRINT
I = 10: J = 15
X = (I = 10): Y = (J = 15) 'X is true (-1); Y is true (-1)
CALL TruthTable(X, Y)
X = (I > 9): Y = (J > 15) 'X is true (-1); Y is false (0)
CALL TruthTable(X, Y)
X = (I <> 10): Y = (J < 16) 'X is false (0); Y is true (-1)
CALL TruthTable(X, Y): X = (I < 10): Y = (J < 15) 'X is false (0); Y is
'false (0)
CALL TruthTable(X, Y)
END
SUB TruthTable (X, Y) STATIC
PRINT X; " "; Y; " "; NOT X; " "; X AND Y; " "; X OR Y;
PRINT " "; X XOR Y; " "; X EQV Y; " "; X IMP Y
PRINT
END SUB
Output
X Y NOT AND OR XOR EQV IMP
-1 -1 0 -1 -1 0 -1 -1
-1 0 0 0 -1 -1 0 0
0 -1 -1 0 -1 -1 0 -1
0 0 -1 0 0 0 -1 -1
Logical operators compare each bit of the first operand with the
corresponding bit in the second operand to compute the bit in the result.
In these bit-wise comparisons, a 0 bit is equivalent to a false value (F),
while a 1 bit is equivalent to a true value (T).
It is possible to use logical operators to test bytes for a particular bit
pattern. For example, the AND operator can be used to mask all but one of
the bits of a status byte, while the OR operator can be used to merge two
bytes to create a particular binary value.
PRINT 63 AND 16
PRINT -1 AND 8
PRINT 10 OR 9
PRINT 10 XOR 10, 'Always 0
PRINT NOT 10, NOT 11, NOT 0 'NOT X = -(X + 1)
Output
16
8
11
0 -11 -12 -1
The first PRINT statement uses AND to combine 63 (111111 binary) and 16
(10000). When BASIC calculates the result of an AND, it combines the
numbers bit by bit, producing a 1 only when both bits are 1. Because the
only bit that is a 1 in both numbers is the fifth bit, only the fifth bit
in the result is a 1. The result is 16, or 10000 in binary.
In the second PRINT statement, the numbers -1 (binary 1111111111111111) and
8 (binary 1000) are combined using another AND operation. The only bit that
is a 1 in both numbers is the fourth bit, so the result is 8 decimal or
1000 binary.
The third PRINT statement uses an OR to combine 10 (binary 1010) and 9
(binary 1001). An OR produces a 1 bit whenever either bit is a 1, so the
result of the OR in the third PRINT is 11 (binary 1011). The XOR in the
fourth PRINT statement combines the number 10 (1010 binary) with itself.
The result is a 0 because an XOR produces a 1 only when either, but not
both, bits are 1.
Performing a NOT on a number changes all 1s to 0s and all 0s to 1s. Because
of the way two's complement numbers work, taking the NOT of a value is the
same as adding one to the number and then negating the number. In the final
PRINT statement, the expression NOT 10 yields a result of -11.