Sign in Join
Wiki copytemporary

Or: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11The [[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.
22
33## boolean operation
44
55The 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`:
66
77| `A` | `B` | `A or B` |
88|---------|---------|----------|
99| `false` | `false` | `false` |
1010| `false` | `true` | `true` |
1111| `true` | `false` | `true` |
1212| `true` | `true` | `true` |
1313
1414## bitwise operation
1515
1616Since 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.
1717
18-`   0101'1010`\
19-`or 0000'1011`\
20-`――――――――――――`\
21-`   0101'1011`
18+```text
19+ 0101'1010
20+or 0000'1011
21+――――――――――――
22+ 0101'1011
23+```
2224
2325For a typical usage example of `or` confer [[&|program messageNo]]’s passing remark.
2426
2527### setting a bit
2628
2729A common task is to set a specific bit. To achieve this utilizing the `or` operator elicits a smart implementation:
2830
2931``` pascal
3032type
3133 integerBitIndex = 0..bitSizeOf(integer)-1;
3234
3335{$push}
3436{$rangeChecks on} // instead of raising an exception, generate an RTE
3537function maskOn(const x: integer; const i: integerBitIndex): integer;
3638begin
3739 maskOn := x or (%1 shl i);
3840end;
3941{$pop}
4042```
4143
4244For example calling `maskOn(%1000, 1)` will result in `%1010` (`%1000` equals decimal eight, and `%1010` is ten).
4345
4446### comparative remarks
4547
4648Note: 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.
4749
4850When 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).
4951
5052## see also
5153
5254- [§ “boolean operators” in the “Free Pascal Reference Guide”](https://www.freepascal.org/docs-html/ref/refsu46.html) regarding the logical disjunction
5355- [§ “logical operators” in the “Free Pascal Reference Guide”](https://www.freepascal.org/docs-html/ref/refsu45.html) regarding the bitwise `or`
5456- [[And|and]]
5557- [[Xor|xor]]
5658- [`system.logicalor`](https://www.freepascal.org/docs-html/rtl/system/.op-logicalor-variant-ariant-ariant.html)
5759
5860---
5961*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).*
6062Category: Code

Back to history