Difference between revisions of "Or"

From Lazarus wiki
Jump to navigationJump to search
m (→‎boolean operation: unlink Plus (avoid multiple links to the same destination in one article))
(add footer AND CLEAN)
(4 intermediate revisions by one other user not shown)
Line 6: Line 6:
  
 
== boolean operation ==
 
== 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.
+
The expression <syntaxhighlight lang="pascal" enclose="none">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.
 
In electrical engineering writing <math>A + B</math> is common, too, but in programming the meaning of the plus sign differs.
 
<syntaxhighlight lang="pascal" enclose="none">A</syntaxhighlight> and <syntaxhighlight lang="pascal" enclose="none">B</syntaxhighlight> are both boolean values.
 
<syntaxhighlight lang="pascal" enclose="none">A</syntaxhighlight> and <syntaxhighlight lang="pascal" enclose="none">B</syntaxhighlight> are both boolean values.
Line 37: Line 37:
 
== 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.
 
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.
+
In FPC the <syntaxhighlight lang="pascal" enclose="none">or</syntaxhighlight> operator is defined appropriately.
 
Such an expression, also known as bitwise or, requires two ordinal operands.
 
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.
 
The operation virtually performs a logical or taking each corresponding bit from both operands.
Line 67: Line 67:
 
Note:
 
Note:
 
The concept of [[Set|sets]] is an integral part of Pascal.
 
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.
+
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.
 
Take it under advisement whether your programming task can be modeled with sets even better.
  
When using sets a [[Plus|plus sign <syntaxhighlight lang="pascal" enclose="none">+</syntaxhighlight>]] virtually does the same as the bitwise <syntaxhighlight lang="pascal" enclose="none">or</syntaxhighlight> does (depends on the compiler's implementation of the data type <syntaxhighlight lang="pascal" enclose="none">set</syntaxhighlight>).
+
When using sets a [[Plus|plus sign {{HL|+}}]] virtually does the same as the bitwise {{HL|or}} does (depending on the compiler's implementation of the data type {{HL|set}}).
 
This notation is, as already mentioned above, familiar from electrical engineering.
 
This notation is, as already mentioned above, familiar from electrical engineering.
However, when handling sets the bitwise <syntaxhighlight lang="pascal" enclose="none">or</syntaxhighlight> is called a ''union'' (of sets).
+
However, when handling sets the bitwise {{HL|or}} is called a ''union'' (of sets).
 
 
== 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>}}
 
  
 +
{{Logical operators}}
 
[[Category:Pascal]]
 
[[Category:Pascal]]
 +
[[Category:Operators]]

Revision as of 23:12, 16 October 2020

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
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 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 (depending 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).


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”