Operators: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Operators
| Old | New | Line | |
|---|---|---|---|
| 1 | + | # Free Pascal operators | |
| 2 | + | ||
| 3 | + | ## Address operator | |
| 4 | + | ||
| 5 | + | The address operator @ returns the start address of a variable, a procedure or a function. If the compiler switch is {\$ T-}, the return value is an untyped pointer. If the compiler switch is {\$ T +}, the return value is a typed pointer. The default setting for the compiler is {\$ T-}. | |
| 6 | + | ||
| 7 | + | ## Assignment operator | |
| 8 | + | ||
| 9 | + | The assignment operator assigns a value to a data field. | |
| 10 | + | ||
| 11 | + | ``` pascal | |
| 12 | + | a := b; // b is assigned to a | |
| 13 | + | ``` | |
| 14 | + | ||
| 15 | + | ## Relational operators | |
| 16 | + | ||
| 17 | + | The following operations are possible: | |
| 18 | + | ||
| 19 | + | - Integer and floating point data fields can be mixed on both sides of the operator. | |
| 20 | + | - Short, ANSI and Widestring data fields can be mixed on both sides of the operator. | |
| 21 | + | ||
| 22 | + | | Operator | Meaning | | |
| 23 | + | |----|----| | |
| 24 | + | | = | equal | | |
| 25 | + | | \<\> | not equal | | |
| 26 | + | | \< | less than | | |
| 27 | + | | \> | greater than | | |
| 28 | + | | \<= | less than or equal | | |
| 29 | + | | \>= | greater or equal | | |
| 30 | + | | in | checks whether the element appears in a set of the same data type | | |
| 31 | + | ||
| 32 | + | ## Binary arithmetic operators | |
| 33 | + | ||
| 34 | + | | Operator | Meaning | | |
| 35 | + | |----|----| | |
| 36 | + | | \+ | addition | | |
| 37 | + | | \- | subtraction | | |
| 38 | + | | * | multiplication | | |
| 39 | + | | / | division | | |
| 40 | + | | div | integer division (Calculates the integer value of how many times an integer is in another integer.) | | |
| 41 | + | | mod | modulo division (Computes the integer remainder of a division of two integers.) | | |
| 42 | + | ||
| 43 | + | ## Unary arithmetic operators | |
| 44 | + | ||
| 45 | + | \+ specifies that the value is positive | |
| 46 | + | ||
| 47 | + | \- specifies that the value is negative | |
| 48 | + | ||
| 49 | + | Examples: | |
| 50 | + | ||
| 51 | + | ``` pascal | |
| 52 | + | a := +7; | |
| 53 | + | b := -3; | |
| 54 | + | ``` | |
| 55 | + | ||
| 56 | + | ## Logical operators | |
| 57 | + | ||
| 58 | + | | Operator | Operation | | |
| 59 | + | |----------|----------------------------------------------------------------| | |
| 60 | + | | not | Bitwise unary negation | | |
| 61 | + | | and | Bitwise and | | |
| 62 | + | | or | Bitwise or | | |
| 63 | + | | xor | Bitwise exclusive or | | |
| 64 | + | | shl | Bitwise shift to the left | | |
| 65 | + | | shr | Bitwise shift to the right | | |
| 66 | + | | \<\< | Bitwise shift to the left (corresponds with the operator shl) | | |
| 67 | + | | \>\> | Bitwise shift to the right (corresponds with the operator shr) | | |
| 68 | + | ||
| 69 | + | Examples: | |
| 70 | + | ||
| 71 | + | ``` pascal | |
| 72 | + | A shr 1 // corresponds to integer DIV 2 (only that it is faster) | |
| 73 | + | // (only for integer data fields, the result is rounded down to an integer) | |
| 74 | + | Not 1 // corresponds to -2 | |
| 75 | + | Not 0 // corresponds to -1 | |
| 76 | + | Not -1 // corresponds to 0 | |
| 77 | + | B shl 2 // corresponds to integer * 4 (only that it is faster) | |
| 78 | + | // (only for integer data fields, the result is an integer) | |
| 79 | + | 1 or 2 // corresponds to 3 | |
| 80 | + | 3 xor 1 // corresponds to 2 | |
| 81 | + | ``` | |
| 82 | + | ||
| 83 | + | ## Truth operators | |
| 84 | + | ||
| 85 | + | | Operator | Operation | | |
| 86 | + | |----------|--------------------------| | |
| 87 | + | | not | logical negation (unary) | | |
| 88 | + | | and | logical and | | |
| 89 | + | | or | logical or | | |
| 90 | + | | xor | logical xor | | |
| 91 | + | ||
| 92 | + | ## String operators | |
| 93 | + | ||
| 94 | + | | Operator | Operation | | |
| 95 | + | |----------|---------------------------------------------------| | |
| 96 | + | | \+ | String concatenation (joins two strings together) | | |
| 97 | + | ||
| 98 | + | ## Set operators | |
| 99 | + | ||
| 100 | + | | Operator | Operation | | |
| 101 | + | |----------|----------------------------------| | |
| 102 | + | | \+ | union | | |
| 103 | + | | \- | difference set | | |
| 104 | + | | * | intersection | | |
| 105 | + | | \>\< | symmetrical difference | | |
| 106 | + | | \<= | contains | | |
| 107 | + | | include | add an item to the set | | |
| 108 | + | | exclude | delete an item in the set | | |
| 109 | + | | in | checks if the item is in the set | | |
| 110 | + | ||
| 111 | + | ## Class Operators | |
| 112 | + | ||
| 113 | + | | Operator | Operation | | |
| 114 | + | |----------|------------------------------------------------------------| | |
| 115 | + | | is | checks whether the object is of a certain class | | |
| 116 | + | | as | performs a conditional type cast (conditional typecasting) | | |
| 117 | + | ||
| 118 | + | ## Pointer Operators | |
| 119 | + | ||
| 120 | + | | Operator | Operation | Operand type | Result type | Example | | |
| 121 | + | |----|----|----|----|----| | |
| 122 | + | | \+ | pointer addition | character pointer, integer | character pointer | P + I | | |
| 123 | + | | \- | pointer subtraction | character pointer, integer | character pointer | P - Q | | |
| 124 | + | | ^ | pointer dereference | pointer | basic type of pointer | P^ | | |
| 125 | + | | = | equal | pointer | boolean \|\| P = Q | | | |
| 126 | + | | \<\> | not equal | pointer | boolean | P \<\> Q | | |
| 127 | + | ||
| 128 | + | ## The priority of the operators | |
| 129 | + | ||
| 130 | + | There is a rigid sequence for processing the operators. | |
| 131 | + | ||
| 132 | + | | Operators | Priority | Category | | |
| 133 | + | |----|----|----| | |
| 134 | + | | Not @ | first (highest) | Unary operators | | |
| 135 | + | | * / div mod and shl shr as \<\< \>\> | second | Multiplication operators | | |
| 136 | + | | \+ - or xor | third | Addition operators | | |
| 137 | + | | = \<\> \< \> \<= \>= in is | fourth (lowest) | Relational operators | | |
| 138 | + | ||
| 139 | + | ## See also | |
| 140 | + | ||
| 141 | + | - [[Pascal basics]] | |
| 142 | + | ||
| 143 | + | --- | |
| 144 | + | *From the Free Pascal wiki, page "Operators", as it stood on 2022-01-17 after a change by Alextp, 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).* | |
| 145 | + | Category: FPC | |
| 146 | + | Category: Pascal |
