Table of Contents

8.4 Logical operators

The group of logical operators NOT, AND, OR, and XOR is required for expressions of the type Boolean on the one hand, and for numbers of the type Short, Integer, or Long (-Integer) on the other. The logical operators form the basis for efficient comparisons and bit manipulation functions. Special cases apply to character strings and objects in logical negation with the NOT operator. Setting parentheses affects the ranking of operations if several logical operators are used.

OperatorDescription
NOT expression0 Calculates the logical NOT of the expression (from True to False and vice versa) or a number based on the binary representation of this number.
Expression1 AND Expression2 Calculates the logical AND of two Boolean expressions or the numeric AND of two integer numbers based on the binary representation of these numbers.
Expression1 OR Expression2 Calculates the logical OR of two Boolean type expressions or the numerical OR of two integer numbers based on the binary representation of these numbers.
Expression1 XOR Expression2 Calculates the logical XOR of two Boolean expressions or the numerical XOR of two integer numbers based on the binary representation of these numbers.

Table 8.4.1: Logical operators for Boolean or numeric expressions

Examples:

If (sCar >= "A" AND sCar <= "Z") OR sCar = "." Then ...
If sPort AND sPort <> "80" Then sReq &= ":" & sPort
IF (x < 0) OR (x <> Round(x)) Then Error.Raise("Mathematic error")
Do While (sSocket.Status <> Net.Connected AND sSocket.Status > 0)

8.4.1 Logical operators for numbers

The numbers are of the type Boolean Short, Integer or Long (-Integer). The calculation with the used logical operator is based on the bitwise logical operation of the binary representation of the numbers on the basis of the so-called truth tables.

The operator inverts each bit in the binary representation of the number, as shown in the following table and examples:

Bit 1Bit 2Not Bit 1Bit 1Bit1 AND Bit2 AND Bit2Bit1 OR Bit1 OR Bit2Bit1 XOR Bit2Bit1 XOR Bit2
0 0 0 0 0 0 1 0
0 1 1 1 0 1 1 1
1 0 0 0 0 1 1 1
1 1 1 1 1 1 0 0

Table 8.4.1.1: Logical operators and truth table

Examples:

Print 13;; Bin(13, 8);; Bin(Not 13, 8);; NOT 13
      13   00001101     11110010         -14
Print 5;; 12;; Bin(5, 8);; Bin(12, 8);; Bin(5 AND 12, 8);; 5 AND 12
      5   12   00000101    00001100     00000100           4
Print 5;; 12;; Bin(5, 8);; Bin(12, 8);; Bin(5 OR 12, 8);; 5 OR 12
      5   12   00000101    00001100     00001101          13
Print 5;; 12;; Bin(5, 8);; Bin(12, 8);; Bin(5 XOR 12, 8);; 5 XOR 12
      5   12   00000101    00001100     00001001           9

Chapter 9.9 Bit manipulation introduces functions that consistently work with the presented logical operators.

8.4.2 Special Case Object and Character String

If the operand after the unary logical operator NOT is a string (string) or an object, the following applies to NOT expression: If the expression is a string or an object, True is returned if the expression is zero or False if the expression is NOT zero.

Examples:

Sub Picture_Write(pPicture As Picture)
    If NOT pPicture Then pPicture = HorizontalFader.DefaultPicture
    MyOriginalPicture = pPicture
    If MyOriginalPicture Then GetPictures
    Me.Draw
End
If NOT btnWithoutText.Text Then Message.Info("Button WITHOUT labelling!")