Difference between revisions of "Or"
(New page: Or produces a value of true if some value is true and false if both of its operands are false. = Bitwise operation = Bitwise or sets the bit to 1 if one corresponding...) 
(replace legacy syntaxhighlight syntax, remove explicit category Category: Pascal already done via {{or}}, add category Code since page contains a code snippet in section →setting a bit: , improve typography) 

(29 intermediate revisions by 9 users not shown)  
Line 1:  Line 1:  
−  Or  +  {{Or}} 
+  The [[Reserved wordreserved word]] <syntaxhighlight lang="pascal" inline>or</syntaxhighlight> is a binary [[Operatoroperator]].  
+  Originally it stood for the logical disjunction of two [[Booleanboolean values]] only, but with the advent of [[Operator overloadingoperator overloading]] [[FPC]] allows everything else, too.  
+  FPC also defines the <syntaxhighlight lang="pascal" inline>or</syntaxhighlight> operator accepting two ordinal types while performing calculations on their internal binary representation.  
−  =  +  == boolean operation == 
+  The expression <syntaxhighlight lang="pascal" inline>A or B</syntaxhighlight> represents the term <math>A \lor B</math> as it is familiar from classical logic.  
+  In electrical engineering writing <math>A + B</math> is common, too, but in programming the meaning of the plus sign differs.  
+  <syntaxhighlight lang="pascal" inline>A</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>B</syntaxhighlight> are both boolean values.  
+  The expression evaluates to either [[false and true<syntaxhighlight lang="pascal" inline>true</syntaxhighlight> or <syntaxhighlight lang="pascal" inline>false</syntaxhighlight>]].  
+  It is only <syntaxhighlight lang="pascal" inline>false</syntaxhighlight> if both operands are <syntaxhighlight lang="pascal" inline>false</syntaxhighlight>:  
−  +  { class="wikitable" style="textalign:center; margin:auto;"  
+  ! <syntaxhighlight lang="pascal" inline>A</syntaxhighlight>  
+  ! <syntaxhighlight lang="pascal" inline>B</syntaxhighlight>  
+  ! <syntaxhighlight lang="pascal" inline>A or B</syntaxhighlight>  
+    
+   <syntaxhighlight lang="pascal" inline>false</syntaxhighlight>  
+   <syntaxhighlight lang="pascal" inline>false</syntaxhighlight>  
+   style="background: #eeeeee"  <syntaxhighlight lang="pascal" inline>false</syntaxhighlight>  
+    
+   <syntaxhighlight lang="pascal" inline>false</syntaxhighlight>  
+   <syntaxhighlight lang="pascal" inline>true</syntaxhighlight>  
+   style="background: #eeeeee"  <syntaxhighlight lang="pascal" inline>true</syntaxhighlight>  
+    
+   <syntaxhighlight lang="pascal" inline>true</syntaxhighlight>  
+   <syntaxhighlight lang="pascal" inline>false</syntaxhighlight>  
+   style="background: #eeeeee"  <syntaxhighlight lang="pascal" inline>true</syntaxhighlight>  
+    
+   <syntaxhighlight lang="pascal" inline>true</syntaxhighlight>  
+   <syntaxhighlight lang="pascal" inline>true</syntaxhighlight>  
+   style="background: #eeeeee"  <syntaxhighlight lang="pascal" inline>true</syntaxhighlight>  
+  + truth table for logical disjunction  
+  }  
−  <  +  == bitwise operation == 
−  function  +  Since virtually all instruction sets have an <syntaxhighlight lang="asm" inline>or</syntaxhighlight> instruction, it is no surprise some highlevel languages, especially those which aim to be suitable for hardware programming, provide some comparable functionality by itself. 
+  In FPC the <syntaxhighlight lang="pascal" inline>or</syntaxhighlight> operator is defined appropriately.  
+  Such an expression, also known as bitwise or, requires two ordinal operands.  
+  The operation virtually performs a logical or taking each corresponding bit from both operands.  
+  0101'1010  
+  or 0000'1011  
+  ――――――――――――  
+  0101'1011  
+  <! You’re welcome to insert a different example where flags are or’ed together instead >  
+  For a typical usage example of <syntaxhighlight lang="pascal" inline>or</syntaxhighlight> confer [[&#octal base<syntaxhighlight lang="pascal" inline>program messageNo</syntaxhighlight>]]’s passing remark.  
+  
+  === setting a bit ===  
+  A common task is to set a specific bit.  
+  To achieve this utilizing the <syntaxhighlight lang="pascal" inline>or</syntaxhighlight> operator elicits a smart implementation:  
+  <syntaxhighlight lang="pascal">  
+  type  
+  integerBitIndex = 0..bitSizeOf(integer)1;  
+  
+  {$push}  
+  {$rangeChecks on} // instead of raising an exception, generate an RTE  
+  function maskOn(const x: integer; const i: integerBitIndex): integer;  
begin  begin  
−  +  maskOn := x or (%1 shl i);  
end;  end;  
−  </delphi>  +  {$pop} 
−  +  </syntaxhighlight>  
+  For example calling <syntaxhighlight lang="delphi" inline>maskOn(%1000, 1)</syntaxhighlight> will result in <syntaxhighlight lang="delphi" inline>%1010</syntaxhighlight> (<syntaxhighlight lang="delphi" inline>%1000</syntaxhighlight> equals decimal eight, and <syntaxhighlight lang="delphi" inline>%1010</syntaxhighlight> is ten).  
+  
+  === comparative remarks ===  
+  Note:  
+  The concept of [[Setsets]] is an integral part of Pascal.  
+  Whilst in other programming languages considering operations on the bit level is not unusual, Pascal provides you with a powerful notion that relieves you from the burden of thinking about bits.  
+  Take it under advisement whether your programming task can be modeled with sets even better.  
+  
+  When using sets a [[Plusplus sign <syntaxhighlight lang="pascal" inline>+</syntaxhighlight>]] virtually does the same as the bitwise <syntaxhighlight lang="pascal" inline>or</syntaxhighlight> does (depends on the compiler’s implementation of the data type <syntaxhighlight lang="pascal" inline>set</syntaxhighlight>).  
+  This notation is, as already mentioned above, familiar from electrical engineering.  
+  However, when handling sets the bitwise <syntaxhighlight lang="pascal" inline>or</syntaxhighlight> is called a ''union'' (of sets).  
+  
+  == see also ==  
+  * [https://www.freepascal.org/docshtml/ref/refsu46.html § “boolean operators” in the “Free Pascal Reference Guide”] regarding the logical disjunction  
+  * [https://www.freepascal.org/docshtml/ref/refsu45.html § “logical operators” in the “Free Pascal Reference Guide”] regarding the bitwise <syntaxhighlight lang="pascal" inline>or</syntaxhighlight>  
+  * [[And<syntaxhighlight lang="pascal" inline>and</syntaxhighlight>]]  
+  * [[Xor<syntaxhighlight lang="pascal" inline>xor</syntaxhighlight>]]  
+  * {{Docpackage=RTLunit=systemidentifier=.oplogicalorvariantariantarianttext=<syntaxhighlight lang="pascal" inline>system.logicalor</syntaxhighlight>}}  
−  +  [[Category:Code]]  
−  
−  
− 
Latest revision as of 16:27, 21 February 2022
│
Deutsch (de) │
English (en) │
suomi (fi) │
français (fr) │
русский (ru) │
The reserved word or
is a binary operator.
Originally it stood for the logical disjunction of two boolean values only, but with the advent of operator overloading FPC allows everything else, too.
FPC also defines the or
operator accepting two ordinal types while performing calculations on their internal binary representation.
boolean operation
The expression A or B
represents the term [math]\displaystyle{ A \lor B }[/math] as it is familiar from classical logic.
In electrical engineering writing [math]\displaystyle{ A + B }[/math] is common, too, but in programming the meaning of the plus sign differs.
A
and B
are both boolean values.
The expression evaluates to either true
or false
.
It is only false
if both operands are false
:
A

B

A or B


false

false

false

false

true

true

true

false

true

true

true

true

bitwise operation
Since virtually all instruction sets have an or
instruction, it is no surprise some highlevel languages, especially those which aim to be suitable for hardware programming, provide some comparable functionality by itself.
In FPC the or
operator is defined appropriately.
Such an expression, also known as bitwise or, requires two ordinal operands.
The operation virtually performs a logical or taking each corresponding bit from both operands.
0101'1010 or 0000'1011 ―――――――――――― 0101'1011
For a typical usage example of or
confer program messageNo
’s passing remark.
setting a bit
A common task is to set a specific bit.
To achieve this utilizing the or
operator elicits a smart implementation:
type
integerBitIndex = 0..bitSizeOf(integer)1;
{$push}
{$rangeChecks on} // instead of raising an exception, generate an RTE
function maskOn(const x: integer; const i: integerBitIndex): integer;
begin
maskOn := x or (%1 shl i);
end;
{$pop}
For example calling maskOn(%1000, 1)
will result in %1010
(%1000
equals decimal eight, and %1010
is ten).
comparative remarks
Note: The concept of sets is an integral part of Pascal. Whilst in other programming languages considering operations on the bit level is not unusual, Pascal provides you with a powerful notion that relieves you from the burden of thinking about bits. Take it under advisement whether your programming task can be modeled with sets even better.
When using sets a plus sign +
virtually does the same as the bitwise or
does (depends on the compiler’s implementation of the data type set
).
This notation is, as already mentioned above, familiar from electrical engineering.
However, when handling sets the bitwise or
is called a union (of sets).
see also
 § “boolean operators” in the “Free Pascal Reference Guide” regarding the logical disjunction
 § “logical operators” in the “Free Pascal Reference Guide” regarding the bitwise
or
and
xor
system.logicalor