Operators in Delphi

Operators behave like predefined functions that are part of the Object Pascal language. For example, the expression (X + Y) is built from the variables X and Y-called operands-with the + operator; when X and Y represent integers or reals, (X + Y) returns their sum. Operators include @, not, ^, *, /, div, mod, and, shl, shr, as, +, -, or, xor, =, >, <, <>, <=, >=, in, and is.
The operators @, not, and ^ are unary (taking one operand). All other operators are binary (taking two operands), except that + and - can function as either unary or binary. A unary operator always precedes its operand (for example, -B), except for ^, which follows its operand (for example, P^). A binary operator is placed between its operands (for example, A = 7).

Types of operators that Delphi you can use in Delphi

Arithmetic operators

Arithmetic operators, which take real or integer operands, include +, -, *, /, div, and mod.

Operator Operation Operand types Result type Example

+ addition integer, real integer, real X + Y
- subtraction integer, real integer, real Result - 1
* multiplication integer, real integer, real P * InterestRate
/ real division integer, real real X / 2
div integer division integer integer Total div UnitSize
mod remainder integer integer Y mod 6
+ (unary) sign identity integer, real integer, real +7
- (unary) sign negation integer, real integer, real -X

Boolean operators

The Boolean operators not, and, or, and xor take operands of any Boolean type and return a value of type Boolean.

Operator Operation Operand types Result type Example
not negation Boolean Boolean not (C in MySet)
and conjunction Boolean Boolean Done and (Total > 0)
or disjunction Boolean Boolean A or B
xor exclusive disjunction Boolean Boolean A xor B

These operations are governed by standard rules of Boolean logic. For example, an expression of the form x and y is True if and only if both x and y are True.

Logical (bitwise) operators

The following logical operators perform bitwise manipulation on integer operands. For example, if the value stored in X (in binary) is 001101 and the value stored in Y is 100001, the statement

Z := X or Y;

assigns the value 101101 to Z.

Operator Operation Operand types Result type Examples
not bitwise negation integer integer not X
and bitwise and integer integer X and Y
or bitwise or integer integer X or Y
xor bitwise xor integer integer X xor Y
shl bitwise shift left integer integer X shl 2
shr bitwise shift right integer integer Y shl I

String operators

The relational operators =, <>, <, >, <=, and >= all take string operands. The + operator concatenates two strings.

Operator Operation Operand types Result type Example
+ concatenation string, packed string, character string S + '. '

Pointer operators

The relational operators <, >, <=, and >= can take operands of type PChar (see Relational operators). The following operators also take pointers as operands.

Operator Operation Operand types Result type Example
+ pointer addition character pointer, integer character pointer P + I
- pointer subtraction character pointer, integer character pointer, integer P - Q
^ pointer dereference pointer base type of pointer P^
= equality pointer Boolean P = Q
<> inequality pointer Boolean P <> Q

Set operators

The following operators take sets as operands.

Operator Operation Operand types Result type Example
+ union set set Set1 + Set2
- difference set set S - T
* intersection set set S * T
<= subset set Boolean Q <= MySet
>= superset set Boolean S1 >= S2
= equality set Boolean S2 = MySet
<> inequality set Boolean MySet <> S1
in membership ordinal, set Boolean A in Set1

Relational operators

Relational operators are used to compare two operands. The operators =, <>, <=, and >= also apply to sets (see Set operators); = and <> also apply to pointers (see Pointer operators).

Operator Operation Operand types Result type Example
= equality simple, class, class reference, Boolean I = Max
interface, string, packed string
<> inequality simple, class, class reference,
interface, string, packed string Boolean X <> Y
< less-than simple, string, packed string, Pchar Boolean X < Y
> greater-than simple, string, packed string, Pchar Boolean Len > 0
<= less-than-or-equal-to simple, string, packed string, Pchar Boolean Cnt <= I
>= greater-than-or-equal-to simple, string, packed string, Pchar Boolean I >= 1

Class operators
The operators as and is take classes and instance objects as operands; as operates on interfaces as well.

The @ operator

The @ operator returns the address of a variable, or of a function, procedure, or method; that is, @ constructs a pointer to its operand.

These are the operators that you can use in Delphi:

'+' = Add.
'-' = Subtract.
'/' = Divide.
'*' = Multiply.
'<' = Less than.
'>' = Greater than.
'<=' = Less than or equal to.
'>=' = Greater than or equal to.
'=' = Equals (test).
':=' = Equals (assign).
':' = Assign a data type to.

Constant expressions

A constant expression is an expression that the compiler can evaluate without executing the program in which it occurs. Constant expressions include numerals; character strings; true constants; values of enumerated types; the special constants True, False, and nil; and expressions built exclusively from these elements with operators, typecasts, and set constructors. Constant expressions cannot include variables, pointers, or function calls, except calls to the following predefined functions:

Abs
Chr
Hi High
Length
Lo Low
Odd
Ord Pred
Round
SizeOf Succ
Swap
Trunc

This definition of a constant expression is used in several places in Object Pascal's syntax specification. Constant expressions are required for initializing global variables, defining subrange types, specifying default parameter values, writing case statements, and declaring both true and typed constants.