'''NOT''' is a [[Boolean]] logical operator that will change a False statement to a True one and vise-versa. 


{{PageSyntax}}
:: 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.


{{Template:RelationalTable}}


{{Template:LogicalTruthTable}}


''Example 1:'' Alternating between two conditions in a program loop.
{{CodeStart}}{{Cl|DO}}
switch = {{Cl|NOT}} switch       '{{Cl|NOT}} changes value from -1 to 0 and vice-versa
{{Cl|LOCATE}} 10, 38
{{Cl|IF}} switch {{Cl|THEN}} {{Cl|PRINT}} &quot;True!&quot; {{Cl|ELSE}} {{Cl|PRINT}} &quot;False&quot;
{{Cl|SLEEP}}
k$ = {{Cl|INKEY$}}
{{Cl|LOOP}} {{Cl|UNTIL}} k$ = {{Cl|CHR$}}(27) ' escape key quit
{{CodeEnd}}


''Example 2:'' Reading a file until it reaches the End Of File.
{{CodeStart}}DO WHILE NOT EOF(1) 
  INPUT #1, data1, data2, data3
LOOP '' ''
{{CodeEnd}}
:''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:
{{CodeStart}}{{Cl|PRINT}} {{Cl|NOT}} 5
{{Cl|PRINT}}
ReadBits 5
ReadBits -6

{{Cl|SUB}} ReadBits (n {{Cl|AS}} {{Cl|INTEGER}}) 'change type value and i bit reads for other whole type values
{{Cl|FOR...NEXT|FOR}} i = 15 {{Cl|TO}} 0 {{Cl|STEP}} -1 'see the 16 bit values
    {{Cl|IF...THEN|IF}} n {{Cl|AND (boolean)|AND}} 2 ^ i {{Cl|THEN}} {{Cl|PRINT}} &quot;1&quot;; {{Cl|ELSE}} {{Cl|PRINT}} &quot;0&quot;;
{{Cl|NEXT}}
{{Cl|PRINT}}
{{Cl|END SUB}} '' ''
{{CodeEnd}}
{{OutputStart}}-6

0000000000000101
1111111111111010
{{OutputEnd}}
:''Explanation:'' The bit values of an [[INTEGER]] are 2 [[_BYTE]]s 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 &quot;1&quot; for bits on or &quot;0&quot; for bits off as text. 

: QB64 can use [[&amp;B]] to convert the above [[_BIT]] values back to [[INTEGER]] or [[_BYTE]] values as shown below:
{{CodeStart}}'16 bit INTEGER values from -32768 to 32767
a% = {{Cl|&amp;B}}0000000000000101
{{Cl|PRINT}} a%
b% = {{Cl|&amp;B}}1111111111111010
{{Cl|PRINT}} b%
'8 bit BYTE values from -128 to 127
a%% = {{Cl|&amp;B}}00000101
{{Cl|PRINT}} a%%
b%% = {{Cl|&amp;B}}11111010
{{Cl|PRINT}} b%%
{{CodeEnd}}


''See also:''
* [[_BIT]], [[&amp;B]], [[_BYTE]]  
* [[AND]], [[XOR]], [[OR]]
* [[Binary]], [[Boolean]] 
* [[Mathematical Operations]]


{{PageNavigation}}