Difference between revisions of "Or"

From Free Pascal wiki
Jump to navigationJump to search
(complete review)
Line 1: Line 1:
 
{{Or}}
 
{{Or}}
  
= Boolean operation =
+
The [[Reserved word|reserved word]] <syntaxhighlight lang="pascal" enclose="none">or</syntaxhighlight> is a binary [[Operator|operator]].
 +
Originally it stood for the logical disjunction of two [[Boolean|boolean values]] only, but with the advent of [[Operator overloading|operator overloading]] [[FPC]] allows everything else, too.
 +
FPC also defines the <syntaxhighlight lang="pascal" enclose="none">or</syntaxhighlight> operator taking two integer types acts on their internal binary representation.
  
Boolean '''Or''' produces a value of [[True|true]] if some value is true and [[False|false]] if both of its Boolean operands are false.
+
== boolean operation ==
 +
The expression <syntaxhighlight lang="pascal" enclose="none">A or B</syntaxhighlight> represents the term <math>A \land B</math> as it is familiar from classical logic.
 +
In electrical engineering writing <math>A + B</math> is common, too, but in programming of the [[Plus|plus-sign]] is different.
 +
<syntaxhighlight lang="pascal" enclose="none">A</syntaxhighlight> and <syntaxhighlight lang="pascal" enclose="none">B</syntaxhighlight> are both boolean values.
 +
The expression evaluates to either [[false and true|<syntaxhighlight lang="pascal" enclose="none">true</syntaxhighlight> or <syntaxhighlight lang="pascal" enclose="none">false</syntaxhighlight>]].
 +
It is only <syntaxhighlight lang="pascal" enclose="none">false</syntaxhighlight> if both operands are <syntaxhighlight lang="pascal" enclose="none">false</syntaxhighlight>:
  
== Truth table ==
+
{| class="wikitable" style="text-align:center; margin:auto;"
 
+
! <syntaxhighlight lang="pascal" enclose="none">A</syntaxhighlight>
{| class="wikitable"
+
! <syntaxhighlight lang="pascal" enclose="none">B</syntaxhighlight>
|-
+
! <syntaxhighlight lang="pascal" enclose="none">A or B</syntaxhighlight>
! A !! B !! A or B
 
 
|-
 
|-
| &nbsp; false &nbsp;|| &nbsp; false &nbsp;
+
| <syntaxhighlight lang="pascal" enclose="none">false</syntaxhighlight>
|style="background: #eeeeee" | &nbsp; false
+
| <syntaxhighlight lang="pascal" enclose="none">false</syntaxhighlight>
 +
| style="background: #eeeeee" | <syntaxhighlight lang="pascal" enclose="none">false</syntaxhighlight>
 
|-
 
|-
| &nbsp; false || &nbsp; true  
+
| <syntaxhighlight lang="pascal" enclose="none">false</syntaxhighlight>
|style="background: #eeeeee" | &nbsp; true
+
| <syntaxhighlight lang="pascal" enclose="none">true</syntaxhighlight>
 +
| style="background: #eeeeee" | <syntaxhighlight lang="pascal" enclose="none">true</syntaxhighlight>
 
|-
 
|-
| &nbsp; true || &nbsp; false  
+
| <syntaxhighlight lang="pascal" enclose="none">true</syntaxhighlight>
|style="background: #eeeeee" | &nbsp; true
+
| <syntaxhighlight lang="pascal" enclose="none">false</syntaxhighlight>
 +
| style="background: #eeeeee" | <syntaxhighlight lang="pascal" enclose="none">true</syntaxhighlight>
 
|-
 
|-
| &nbsp; true || &nbsp; true  
+
| <syntaxhighlight lang="pascal" enclose="none">true</syntaxhighlight>
|style="background: #eeeeee" | &nbsp; true
+
| <syntaxhighlight lang="pascal" enclose="none">true</syntaxhighlight>
 +
| style="background: #eeeeee" | <syntaxhighlight lang="pascal" enclose="none">true</syntaxhighlight>
 +
|+ truth table for logical disjunction
 
|}
 
|}
  
= Bitwise operation =
+
== bitwise operation ==
 +
Since virtually all instruction sets have an <syntaxhighlight lang="asm" enclose="none">or</syntaxhighlight> instruction, it is no surprise some high-level languages, especially those which aim to be suitable for hardware programming, provide some comparable functionality by itself.
 +
In FPC the <syntaxhighlight lang="pascal" enclose="none">or</syntaxhighlight> operator 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
  
Logical '''Or''' (aka Bitwise Or) requires ordinal operands and sets and a bit in the result variable to 1 if one corresponding bit in its operands is 1, and to 0 if both are 0.
+
=== setting a bit ===
 +
A common task is to set a specific bit.
 +
To achieve this utilizing the <syntaxhighlight lang="pascal" enclose="none">or</syntaxhighlight> operator elicits a smart implementation:
 +
<syntaxhighlight lang="pascal">
 +
type
 +
integerBitIndex = 0..bitSizeOf(integer)-1;
  
== Set a bit ==
+
{$push}
<syntaxhighlight>
+
{$rangeChecks on} // instead of an exception will generate an RTE
function SetBit(const AValue, ABitNumber:integer):integer;
+
function maskOn(const x: integer; const i: integerBitIndex): integer;
 
begin
 
begin
  result := AValue or (1 shl ABitNumber);
+
maskOn := x or (%1 shl i);
 
end;
 
end;
 +
{$pop}
 
</syntaxhighlight>
 
</syntaxhighlight>
If you call SetBit(%1000,1) then get %1010 (%1000 = 8 and %1010 = 10). If you call SetBit(10,2) then get 14 (14 = %1110). If you call SetBit(10,1) then get 10.
+
For example calling <syntaxhighlight lang="delphi" enclose="none">maskOn(%1000, 1)</syntaxhighlight> will result in <syntaxhighlight lang="delphi" enclose="none">%1010</syntaxhighlight> (<syntaxhighlight lang="delphi" enclose="none">%1000</syntaxhighlight> equals decimal eight, and <syntaxhighlight lang="delphi" enclose="none">%1010</syntaxhighlight> is ten).
 +
 
 +
=== comparative remarks ===
 +
Note:
 +
The concept of [[Set|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 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.
 +
 
 +
== see also ==
 +
* [https://www.freepascal.org/docs-html/ref/refsu47.html § “boolean operators” in the “Free Pascal Reference Guide”] regarding the logical disjunction
 +
* [https://www.freepascal.org/docs-html/ref/refsu46.html § “logical operators” in the “Free Pascal Reference Guide”] regarding the bitwise <syntaxhighlight lang="pascal" enclose="none">or</syntaxhighlight>
 +
* [[And|<syntaxhighlight lang="pascal" enclose="none">and</syntaxhighlight>]]
 +
* {{Doc|package=RTL|unit=system|identifier=.op-logicalor-variant-ariant-ariant|text=<syntaxhighlight lang="pascal" enclose="none">system.logicalor</syntaxhighlight>}}
  
== See also ==
+
[[Category:Pascal]]
* [[And]]
 
* [[Const]]
 
* [[Function]]
 
* [[Integer]]
 
* [[Shl]]
 
* [[Shr]]
 
<br>
 
<br>
 
[[category:Pascal]]
 

Revision as of 14:39, 26 October 2018

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 taking two integer types acts on their internal binary representation.

boolean operation

The expression A or B represents the term [math]\displaystyle{ A \land B }[/math] as it is familiar from classical logic. In electrical engineering writing [math]\displaystyle{ A + B }[/math] is common, too, but in programming of the plus-sign is different. 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
truth table for logical disjunction

bitwise operation

Since virtually all instruction sets have an or instruction, it is no surprise some high-level languages, especially those which aim to be suitable for hardware programming, provide some comparable functionality by itself. In FPC the or operator 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

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 an exception will 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 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.

see also