NOT operator

Purpose

The NOT operator works as a bitwise arithmetic operator.

Syntax

NOT p

Remarks

PowerBASIC's NOT operator returns the one's-complement of an integer-class expression.  When dealing with the absolute values 0 and -1, the NOT operator "reverses" the two values, performing a Boolean-like operation.

PowerBASIC accepts any non-zero value as a logical TRUE value; therefore, subtle logic problems can arise in a program when the NOT operator is used to perform Boolean logic tests with operand values that are not limited to just 0 and -1.

Consider the following two test conditions:

test1 = 0          ' test1 is FALSE (zero)

IF NOT test1 THEN  ' TRUE (-1 is non-zero)

 

test2 = 1          ' test2 is TRUE (1 is non-zero)

IF NOT test2 THEN  ' still TRUE (-2 is non-zero)

Because NOT performs a bitwise operation on test2, it does not reverse the logical TRUE/FALSE value of test2, rather, it returns -2 (the one's-complement of 1) and this is evaluated as a logical TRUE value.

In cases where a proper logical (Boolean) evaluation is required, and the operand may be a value other than 0 and -1, the ISFALSE operator should be used in place of the NOT operator:

test3 = 1             ' test3 is TRUE (non-zero)

IF ISFALSE test3 THEN ' ISFALSE detects test3 is

...                   ' TRUE so the IF test fails

The two's-complement of a value can be obtained with the following algorithm:

y = (NOT x) + 1

Using NOT as a logical operator

NOT returns 0 (FALSE) if and only if its operand is exactly -1 (TRUE).  Generally, you should use the ISFALSE operator instead of NOT, when you are testing for logical falsity.

Truth table

x

NOT x

 0

-1

-1

0

Using NOT as a bitwise arithmetic operator

NOT performs a one's-complement or bit reversal of each bit in an integer-class value.  Here is a sample:

x% = NOT 16383%  ' Result is -16384

See also

Arithmetic OperatorsAND, EQV, IMP, ISFALSEISTRUEOR, Short-circuit evaluation, XOR