Or: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 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 | 2 | ||
| 3 | 3 | ## boolean operation | |
| 4 | 4 | ||
| 5 | 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 | 6 | ||
| 7 | 7 | | `A` | `B` | `A or B` | | |
| 8 | 8 | |---------|---------|----------| | |
| 9 | 9 | | `false` | `false` | `false` | | |
| 10 | 10 | | `false` | `true` | `true` | | |
| 11 | 11 | | `true` | `false` | `true` | | |
| 12 | 12 | | `true` | `true` | `true` | | |
| 13 | 13 | ||
| 14 | 14 | ## bitwise operation | |
| 15 | 15 | ||
| 16 | 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 | 17 | ||
| 18 | - | ||
| 19 | - | ||
| 20 | - | ||
| 21 | - | ||
| 18 | + | ```text | |
| 19 | + | 0101'1010 | |
| 20 | + | or 0000'1011 | |
| 21 | + | ―――――――――――― | |
| 22 | + | 0101'1011 | |
| 23 | + | ``` | |
| 22 | 24 | ||
| 23 | 25 | For a typical usage example of `or` confer [[&|program messageNo]]’s passing remark. | |
| 24 | 26 | ||
| 25 | 27 | ### setting a bit | |
| 26 | 28 | ||
| 27 | 29 | A common task is to set a specific bit. To achieve this utilizing the `or` operator elicits a smart implementation: | |
| 28 | 30 | ||
| 29 | 31 | ``` pascal | |
| 30 | 32 | type | |
| 31 | 33 | integerBitIndex = 0..bitSizeOf(integer)-1; | |
| 32 | 34 | ||
| 33 | 35 | {$push} | |
| 34 | 36 | {$rangeChecks on} // instead of raising an exception, generate an RTE | |
| 35 | 37 | function maskOn(const x: integer; const i: integerBitIndex): integer; | |
| 36 | 38 | begin | |
| 37 | 39 | maskOn := x or (%1 shl i); | |
| 38 | 40 | end; | |
| 39 | 41 | {$pop} | |
| 40 | 42 | ``` | |
| 41 | 43 | ||
| 42 | 44 | For example calling `maskOn(%1000, 1)` will result in `%1010` (`%1000` equals decimal eight, and `%1010` is ten). | |
| 43 | 45 | ||
| 44 | 46 | ### comparative remarks | |
| 45 | 47 | ||
| 46 | 48 | 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 | 49 | ||
| 48 | 50 | 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 | 51 | ||
| 50 | 52 | ## see also | |
| 51 | 53 | ||
| 52 | 54 | - [§ “boolean operators” in the “Free Pascal Reference Guide”](https://www.freepascal.org/docs-html/ref/refsu46.html) regarding the logical disjunction | |
| 53 | 55 | - [§ “logical operators” in the “Free Pascal Reference Guide”](https://www.freepascal.org/docs-html/ref/refsu45.html) regarding the bitwise `or` | |
| 54 | 56 | - [[And|and]] | |
| 55 | 57 | - [[Xor|xor]] | |
| 56 | 58 | - [`system.logicalor`](https://www.freepascal.org/docs-html/rtl/system/.op-logicalor-variant-ariant-ariant.html) | |
| 57 | 59 | ||
| 58 | 60 | --- | |
| 59 | 61 | *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 | 62 | Category: Code |
