From QB64 Wiki
NOT is a Boolean logical operator that will change a False statement to a True one and vise-versa.
- True = -1: False = NOT True
- In Qbasic, True = -1 and False = 0 in boolean logic and evaluation statements.
- NOT evaluates ONE value and returns the opposite. Yes, NOT 0 = -1 in Basic.
- Often called a negative logic operator, it returns the opposite of a value as true or false.
- Values are changed by their bit values so that each bit is changed to the opposite of on or off. See example 3.
|<||Less than||IF a < b THEN|
|>||Greater than||IF a > b THEN|
|=||Equal||IF a = b THEN|
|<=||Less than or equal||IF a <= b THEN|
|>=||Greater than or equal||IF a >= b THEN|
|<>||NOT equal||IF a <> b THEN|
Operands Operations A B NOT B A AND B A OR B A XOR B A EQV B A IMP B T T F T T F T T T F T F T T F F F T F F T T F T F F T F F F T T
Example 1: Alternating between two conditions in a program loop.
Example 2: Reading a file until it reaches the End Of File.
DO WHILE NOT EOF(1) INPUT #1, data1, data2, data3 LOOP
- Explanation: EOF will return 0 until a file ends. NOT converts 0 to -1 so that the loop continues to run. When EOF becomes -1, NOT converts it to 0 to end the loop.
Example 3: So why does NOT 5 = -6? Because NOT changes every bit of a value into the opposite:
PRINT NOT 5 PRINT ReadBits 5 ReadBits -6 SUB ReadBits (n AS INTEGER) 'change type value and i bit reads for other whole type values FOR i = 15 TO 0 STEP -1 'see the 16 bit values IF n AND 2 ^ i THEN PRINT "1"; ELSE PRINT "0"; NEXT PRINT END SUB
-6 0000000000000101 1111111111111010
- Explanation: The bit values of an INTEGER are 2 _BYTEs and each bit is an exponent of 2 from 15 to 0 (16 bits). Thus comparing the numerical value with those exponents using AND reveals the bit values as "1" for bits on or "0" for bits off as text.