Difference between revisions of "Not"

From Lazarus wiki
Jump to navigationJump to search
(c)
(add footer, cat)
 
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{not}}
 
{{not}}
<br>
 
<br>
 
__TOC__
 
= Boolean operation =
 
  
'''Not''' produces a value of [[True|true]] if original value is [[False|false]].
+
The unary [[Operator|operator]] <syntaxhighlight lang="pascal" inline>not</syntaxhighlight> negates a Boolean value.
 +
[[FPC]] also knows the bitwise <syntaxhighlight lang="pascal" inline>not</syntaxhighlight> when supplied with an ordinal type.
  
== Truth table ==
+
<syntaxhighlight lang="pascal" inline>not</syntaxhighlight> is a [[Reserved word|reserved word]].
  
{| class="wikitable"
+
== Boolean operation ==
|-
+
 
! A !! Not A  
+
The operator <syntaxhighlight lang="pascal" inline>not</syntaxhighlight> represents the logical negation <math>\neg A</math>.
 +
In electrical engineering one might write <math>-A</math> or <math>\overline{A}</math> instead, however the unary [[Minus|minus sign]] has a different meaning in programming.
 +
 
 +
{| class="wikitable" style="text-align:center; margin:auto;"
 +
! <syntaxhighlight lang="pascal" inline>A</syntaxhighlight>
 +
! <syntaxhighlight lang="pascal" inline>not A</syntaxhighlight>
 
|-
 
|-
| &nbsp; false &nbsp;
+
| <syntaxhighlight lang="pascal" inline>false</syntaxhighlight>
|style="background: #eeeeee" | &nbsp; true
+
| style="background: #eeeeee" | <syntaxhighlight lang="pascal" inline>true</syntaxhighlight>
 
|-
 
|-
| &nbsp; true &nbsp;
+
| <syntaxhighlight lang="pascal" inline>true</syntaxhighlight>
|style="background: #eeeeee" | &nbsp; false &nbsp;
+
| style="background: #eeeeee" | <syntaxhighlight lang="pascal" inline>false</syntaxhighlight>
 +
|+ Truth table for logical negation
 
|}
 
|}
  
 +
<syntaxhighlight lang="pascal" inline>not</syntaxhighlight> has the highest precedence among logical operators.
  
= Bitwise operation =
+
== Bitwise operation ==
 
 
Bitwise '''not''' sets the bit to 1 if corresponding bit is 0, and to 0 if bit is 1.
 
 
 
== Ones' complement ==
 
 
 
<syntaxhighlight>
 
function OnesComplement ( const aValue : byte ): byte;
 
begin
 
  result := Not AValue;
 
end;
 
</syntaxhighlight>
 
If you call OnesComplement([[Percent sign|%]]10000000) then get %01111111 (%10000000 = 128 and %01111111 = 127). If you call OnesComplement(%00000111) then get 248 (248 = %11111000).
 
 
 
 
 
<syntaxhighlight>
 
function OnesComplement2 ( const aValue : shortint ): shortint;
 
begin
 
  result := Not AValue;
 
end;
 
</syntaxhighlight>
 
  
If you call OnesComplement2(%00000010) then get %11111101 (%00000010 = 2 and %11111101 = -3 when [[Type|type]] is shortint). If you call OnesComplement2(7) then get -8 (-8 = %11111000  when type is shortint and 7 = %00000111 ).  
+
The bitwise <syntaxhighlight lang="pascal" inline>not</syntaxhighlight> flips every bit in an ordinal type.
 +
not 1100'1010
 +
―――――――――――――
 +
    0011'0101
 +
It effectively calculates the one’s complement.
 +
On virtually all platforms it is implemented by the <syntaxhighlight lang="asm" inline>not</syntaxhighlight> instruction.
 +
On NAND-gate-based architectures the <syntaxhighlight lang="asm" inline>not</syntaxhighlight> instruction can be calculated by the expression <math>A &#8965; A</math>.
  
 +
Note, that only <syntaxhighlight lang="pascal" inline>not %0</syntaxhighlight> will ''definitely'' result in a value interpretable as <syntaxhighlight lang="pascal" inline>true</syntaxhighlight>.
 +
However, not every other <syntaxhighlight lang="pascal" inline>not x</syntaxhighlight> will result in a value interpretable as <syntaxhighlight lang="pascal" inline>false</syntaxhighlight>, since only <syntaxhighlight lang="pascal" inline>0</syntaxhighlight> is considered as <syntaxhighlight lang="pascal" inline>false</syntaxhighlight> and every other value as <syntaxhighlight lang="pascal" inline>true</syntaxhighlight>.
 +
For example, <syntaxhighlight lang="delphi" inline>boolean(not %1)</syntaxhighlight> will evaluate as <syntaxhighlight lang="pascal" inline>true</syntaxhighlight>, but only <syntaxhighlight lang="delphi" inline>boolean(not high(nativeUInt))</syntaxhighlight> will evaluate to <syntaxhighlight lang="pascal" inline>false</syntaxhighlight>.
  
== Read more ==
+
{{Logical operators}}
* [[Shl]]
+
* {{Doc|package=RTL|unit=system|identifier=.op-logicalnot-variant-ariant|text=<syntaxhighlight lang="pascal" inline>system.logicalNot</syntaxhighlight>}}
* [[Const]]
 
* [[Function]]
 
* [[Byte]]
 
* [[Shortint]]
 
* [[Shl#Clear_a_bit| Clear_a_bit]] (bitwise example)
 
  
[[Category:Pascal]]
+
[[Category:Operators]]

Latest revision as of 23:14, 16 October 2020

Deutsch (de) English (en) suomi (fi) français (fr) русский (ru)

The unary operator not negates a Boolean value. FPC also knows the bitwise not when supplied with an ordinal type.

not is a reserved word.

Boolean operation

The operator not represents the logical negation [math]\displaystyle{ \neg A }[/math]. In electrical engineering one might write [math]\displaystyle{ -A }[/math] or [math]\displaystyle{ \overline{A} }[/math] instead, however the unary minus sign has a different meaning in programming.

A not A
false true
true false
Truth table for logical negation

not has the highest precedence among logical operators.

Bitwise operation

The bitwise not flips every bit in an ordinal type.

not 1100'1010
―――――――――――――
    0011'0101

It effectively calculates the one’s complement. On virtually all platforms it is implemented by the not instruction. On NAND-gate-based architectures the not instruction can be calculated by the expression [math]\displaystyle{ A &#8965; A }[/math].

Note, that only not %0 will definitely result in a value interpretable as true. However, not every other not x will result in a value interpretable as false, since only 0 is considered as false and every other value as true. For example, boolean(not %1) will evaluate as true, but only boolean(not high(nativeUInt)) will evaluate to false.


navigation bar: Pascal logical operators
operators

and • or • not • xor
shl • shr
and_then (N/A)• or_else (N/A)

see also

{$boolEval} • Reference: § “boolean operators” • Reference: § “logical operators”