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.
Logical Operators Example
◄Example► ◄Contents► ◄Index► ◄Back►
──────────────────────────────────────────────────────────────────────────────
' This example uses logical operators to 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.
' To try this example:
' 1. Choose New Project from the File menu
' 2. Copy the code example below to the code window
' 3. Press F5 to run the example
CLS ' Clear the screen
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)
' The first PRINT statement uses AND to combine 63 (111111 binary) and 16
' (10000). When Visual 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.
STATIC SUB TruthTable (X, Y)
PRINT X; " "; Y; " "; NOT X; " "; X AND Y; " "; X OR Y;
PRINT " "; X XOR Y; " "; X EQV Y; " "; X IMP Y
PRINT
END SUB