Q(uick)BASIC Operators: Boolean

Quick View

Boolean Operators

Boolean operators perform bit manipulations, Boolean operations, or tests on multiple relations. They return a true (nonzero) or false (zero) value to be used in making a decision

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • result = expression1 boolean-operator expression2
Description/Parameter(s)
boolean-operator Any of the following Boolean operators:
NOT Bit-wise complement
AND Conjunction
OR Disjunction (inclusive "or")
XOR Exclusive "or"
EQV Equivalence
IMP Implication

Each operator returns results as indicated in the following truth table. T is true (nonzero); F is false (zero):

Expression1 Expression2 NOT AND OR XOR EQV IMP
T T F T T F T T
T F F F T T F F
F T T F T T F T
F F T F F F T T
  • Boolean operations are performed after arithmetic and relational operations in order of precedence.
  • Expressions are converted to integers or long integers before a Boolean operation is performed.
  • If the expressions evaluate to 0 or -1, a Boolean operation returns 0 or -1 as the result. Because Boolean operators do bit-wise calculations, using values other than 0 for false and -1 for true may produce unexpected results.
Syntax
  • result = expression1 boolean-operator expression2
Description/Parameter(s)

The six logical operators in BASIC, listed in order of precedence, are:

Operator Meaning
NOT Logical complement
AND Conjunction
OR Disjunction (inclusive "or")
XOR Exclusive "or"
EQV Equivalence
IMP Implication

Each operator returns results as indicated below. A "T" indicates a true value and an "F" indicates a false value. Operators are listed in order of operator precedence.

Values of Value Returned by Logical Operator
X X X X X
NOT AND OR XOR EQV IMP
X Y X Y Y Y Y Y
T T F T T F T T
T F F F T T F F
F T T F T T F T
F F T F F F T T

In an expression, logical operations (also known as Boolean operations) are performed after arithmetic and relational operations. The operands of logical operators must be in the range -2,147,483,648 to +2,147,483,647. Operands are converted to integers (or, if necessary, long integers) before the logical operation is done. (If the operands are not in this range, an error results.) If the operands are either 0 or -1, logical operators return 0 or -1 as the result.

Example
IF D < 200 AND F < 4 THEN 80 WHILE I > 10 OR K < 0 ' ' ' WEND IF NOT P THEN PRINT "Name not found"
Syntax
  • result = expression1 boolean-operator expression2
Description/Parameter(s)

The six logical operators in BASIC, listed in order of precedence, are as follows:

Operator Meaning
NOT Logical complement
AND Conjunction
OR Disjunction (inclusive "or")
XOR Exclusive "or"
EQV Equivalence
IMP Implication

In an expression, logical operations (also known as Boolean operations) are performed after arithmetic and relational operations. Operands are converted to integers (or, if necessary, long integers) before the logical operation is done. The operands of logical operators must be in the range -2,147,483,648 to 2,147,483,647 (if the operands are not in this range, an error results.) If the operands are either 0 or -1, logical operators return 0 or -1 as the result.

Truth Table for Logical Operators

Each operator returns results as indicated below. A "T" indicates a true value and an "F" indicates a false value. Operators are listed in order of operator precedence.

Values of Value Returned by Logical Operator
X X X X X
NOT AND OR XOR EQV IMP
X Y X Y Y Y Y Y
T T F T T F T T
T F F F T T F F
F T T F T T F T
F F T F F F T T
Examples

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

Sample 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)

Sample 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.