NOT - mkilgore/QB64pe GitHub Wiki
NOT is a boolean logical operator that will change a false statement to a true one and vice-versa.
- True = -1: False = NOT True
- In QBasic, True = -1 and False = 0 in boolean logic and evaluation statements.
- NOT evaluates a value and returns the bitwise opposite, meaning that NOT 0 = -1.
- 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 below.
Symbol | Condition | Example Usage |
---|---|---|
< | 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 |
The results of the bitwise logical operations, where A and B are operands, and true or false indicate whether a bit is set or not set:
| ||||||||||||||||||||||||||||||||||||||||||||||||
Relational Operations return negative one (-1, all bits set) and zero (0, no bits set) for true and false, respectively. This allows relational tests to be inverted and combined using the bitwise logical operations. |
Example 1: Alternating between two conditions in a program loop.
DO switch = NOT switch 'NOT changes value from -1 to 0 and vice-versa LOCATE 10, 38 IF switch THEN PRINT "True!" ELSE PRINT "False" SLEEP k$ = INKEY$ LOOP UNTIL k$ = CHR$(27) ' escape key quit |
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.
SUB ReadBits (n AS INTEGER) 'change type value and i bit reads for other whole type values FOR...NEXT i = 15 TO 0 STEP -1 'see the 16 bit values IF...THEN n AND (boolean) 2 ^ i THEN PRINT "1"; ELSE PRINT "0"; NEXT PRINT END SUB '' '' |
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.
- QB64 can use &B to convert the above _BIT values back to INTEGER or _BYTE values as shown below:
'16 bit INTEGER values from -32768 to 32767 a% = &B0000000000000101 PRINT a% b% = &B1111111111111010 PRINT b% '8 bit BYTE values from -128 to 127 a%% = &B00000101 PRINT a%% b%% = &B11111010 PRINT b%% |
Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page