Or: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Or
| Old | New | Line | |
|---|---|---|---|
| 1 | + | The [[Reserved word|reserved word]] `or` 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 `or` operator accepting two ordinal types while performing calculations on their internal binary representation. | |
| 2 | + | ||
| 3 | + | ## boolean operation | |
| 4 | + | ||
| 5 | + | The expression `A or B` represents the term $`A \lor B`$ as it is familiar from classical logic. In electrical engineering writing $`A + B`$ 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 [[false and true|true or false]]. It is only `false` if both operands are `false`: | |
| 6 | + | ||
| 7 | + | | `A` | `B` | `A or B` | | |
| 8 | + | |---------|---------|----------| | |
| 9 | + | | `false` | `false` | `false` | | |
| 10 | + | | `false` | `true` | `true` | | |
| 11 | + | | `true` | `false` | `true` | | |
| 12 | + | | `true` | `true` | `true` | | |
| 13 | + | ||
| 14 | + | ## bitwise operation | |
| 15 | + | ||
| 16 | + | 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. | |
| 17 | + | ||
| 18 | + | ` 0101'1010`\ | |
| 19 | + | `or 0000'1011`\ | |
| 20 | + | `――――――――――――`\ | |
| 21 | + | ` 0101'1011` | |
| 22 | + | ||
| 23 | + | For a typical usage example of `or` confer [[&|program messageNo]]’s passing remark. | |
| 24 | + | ||
| 25 | + | ### setting a bit | |
| 26 | + | ||
| 27 | + | A common task is to set a specific bit. To achieve this utilizing the `or` operator elicits a smart implementation: | |
| 28 | + | ||
| 29 | + | ``` pascal | |
| 30 | + | type | |
| 31 | + | integerBitIndex = 0..bitSizeOf(integer)-1; | |
| 32 | + | ||
| 33 | + | {$push} | |
| 34 | + | {$rangeChecks on} // instead of raising an exception, generate an RTE | |
| 35 | + | function maskOn(const x: integer; const i: integerBitIndex): integer; | |
| 36 | + | begin | |
| 37 | + | maskOn := x or (%1 shl i); | |
| 38 | + | end; | |
| 39 | + | {$pop} | |
| 40 | + | ``` | |
| 41 | + | ||
| 42 | + | For example calling `maskOn(%1000, 1)` will result in `%1010` (`%1000` equals decimal eight, and `%1010` is ten). | |
| 43 | + | ||
| 44 | + | ### comparative remarks | |
| 45 | + | ||
| 46 | + | 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 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. | |
| 47 | + | ||
| 48 | + | When using sets a [[Plus|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). | |
| 49 | + | ||
| 50 | + | ## see also | |
| 51 | + | ||
| 52 | + | - [§ “boolean operators” in the “Free Pascal Reference Guide”](https://www.freepascal.org/docs-html/ref/refsu46.html) regarding the logical disjunction | |
| 53 | + | - [§ “logical operators” in the “Free Pascal Reference Guide”](https://www.freepascal.org/docs-html/ref/refsu45.html) regarding the bitwise `or` | |
| 54 | + | - [[And|and]] | |
| 55 | + | - [[Xor|xor]] | |
| 56 | + | - [`system.logicalor`](https://www.freepascal.org/docs-html/rtl/system/.op-logicalor-variant-ariant-ariant.html) | |
| 57 | + | ||
| 58 | + | --- | |
| 59 | + | *From the Free Pascal wiki, page "Or", as it stood on 2022-02-21 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 60 | + | Category: Code |
