mirror of
https://github.com/QB64Official/qb64.git
synced 2024-08-22 02:05:08 +00:00
82 lines
2.8 KiB
Text
82 lines
2.8 KiB
Text
[[NOT]] is a [[Boolean|boolean]] logical operator that will change a false statement to a true one and vice-versa.
|
|
|
|
|
|
{{PageSyntax}}
|
|
: ''True'' = -1: ''False'' = [[NOT]] True
|
|
|
|
|
|
{{PageDescription}}
|
|
* In QBasic, True = -1 and False = 0 in boolean logic and evaluation statements.
|
|
* [[NOT]] evaluates a value and returns the bitwise opposite, meaning that {{InlineCode}}NOT 0 = -1{{InlineCodeEnd}}.
|
|
* 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.
|
|
|
|
|
|
{{Template:RelationalTable}}
|
|
|
|
|
|
{{Template:LogicalTruthTable}}
|
|
|
|
|
|
{{PageExamples}}
|
|
''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}} "True!" {{Cl|ELSE}} {{Cl|PRINT}} "False"
|
|
{{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}} "1"; {{Cl|ELSE}} {{Cl|PRINT}} "0";
|
|
{{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 "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:
|
|
{{CodeStart}}'16 bit INTEGER values from -32768 to 32767
|
|
a% = {{Cl|&B}}0000000000000101
|
|
{{Cl|PRINT}} a%
|
|
b% = {{Cl|&B}}1111111111111010
|
|
{{Cl|PRINT}} b%
|
|
'8 bit BYTE values from -128 to 127
|
|
a%% = {{Cl|&B}}00000101
|
|
{{Cl|PRINT}} a%%
|
|
b%% = {{Cl|&B}}11111010
|
|
{{Cl|PRINT}} b%%
|
|
{{CodeEnd}}
|
|
|
|
|
|
{{PageSeeAlso}}
|
|
* [[_BIT]], [[&B]], [[_BYTE]]
|
|
* [[AND]], [[XOR]], [[OR]]
|
|
* [[Binary]], [[Boolean]]
|
|
* [[Mathematical Operations]]
|
|
|
|
|
|
{{PageNavigation}}
|