Numerical Operations and Operators
- Updated2025-10-31
- 4 minute(s) read
Numerical Operations and Operators
Use mathematical operators, bit operators, and self-assigned operators in PAscript.
Mathematical Operators
Use mathematical operators for integers and floating-point numbers.
If floating point and integer numbers are included in a calculation, you must indicate appropriate type conversions in the calculation. Refer to Type Conversions for more information.
| Operator | Operation | Example | Comment |
|---|---|---|---|
| + | Addition | y=x+1 | Variable y is assigned the value of x+1. |
| - | Subtraction | x=y-1 | Variable x is assigned the value of y-1 . |
| / | Division | y=x/10 | Variable y is assigned the value of x divided by 10 |
| * | Multiplication | y=x*a | Variable y is assigned the value of x times a. |
| mod | Modulo | y=x mod 2 | Variable y is assigned the remainder of the division of x divided by 2. |
| ++ | Increment by 1 | x++ | Variable x is incremented by 1. This is identical to the statement x=x+1. |
| -- | Decrement by 1 | y=x-- | Variable x is assigned to variable y and then decremented by 1. This is identical to the statements y=x and x=x-1. |
| += | Shorthand notation | a = a + ... | Shorthand notation for a = a + .... |
| -= | Shorthand notation | a = a - ... | Shorthand notation for a = a - .... |
| /= | Shorthand notation | a = a / ... | Shorthand notation for a = a / .... |
| *= | Shorthand notation | a = a * ... | Shorthand notation for a = a * .... |
| &= | Shorthand notation | a = a & ... | Shorthand notation for a = a & .... |
| |= | Shorthand notation | a = a | ... | Shorthand notation for a = a | .... |
| ^= | Shorthand notation | a = a ^ ... | Shorthand notation for a = a ^ .... |
The following guidelines apply to incrementing and decrementing.
- Use incrementing and decrementing for arguments in function calls, loops, calculations or if conditions.
- When using a variable in combination with incrementing or decrementing, the variable is used first. Then the variable is incremented or decremented. The variable that is incremented or decremented must never be the variable on the left side of an assignment. For example, the compiler does not evaluate expressions such as y=y++ or y=x+y--.
Example: Mathematical Operators
program int32 x=10, y=0 y=x++ // => y=x, x=x+1 sys:logInfo( sys:ui, "x=%d, y=%d", x, y ) // => x=11, y=10 incDemo ( y++ ) // => a=10 sys:logInfo( sys:ui, "y=%d", y ) // => y=11 y=10+x++ // => y=10+x, x=x+1 sys:logInfo ( sys:ui, "x=%d, y=%d", x, y ) // => x=12, y=21 endprogram function incDemo( int32 a ) sys:logInfo ( sys:ui, "a=%d", a ) // => a=10 endfunction
Bit Operators
Bit operators can only be used with integer data types such as int32.
| Operator | Operation | Example | Comment |
|---|---|---|---|
| & | Bitwise and | y=i&3 | Variable y is assigned the lowest two bits of variable i. |
| | | Bitwise or | y=i|j | The bits of the variables i or j are compared individually. The higher-value bit is assigned to the variable y. |
| ^ | Bitwise exclusive or | y=i^j | In variable y, only those bits are set that are set exclusively in one of the two variables i or j. |
| << | Shift bits to the left | y=i<<3 | The bits of variable i are moved three places in the direction of higher bits. The bits are assigned to variable y. |
| >> | Shift bits to the right | y=i>>j | The bits of variable i are shifted by j places in the direction of lower bits. The bits are assigned to variable y. |
Bitwise working operators can be chained to each other as desired.
All integer data types that can only be used for local variables are supported.
Example: Bit Operators
program
int32y,i=3,j=2,k=1
y=(j+k) & i // => bit comparison of i with j+k
sys: logInfo (sys:ui, "y=%d, i=%d, j=%d, k=%d", y,i,j,k) // => y=3, i=3, j=2, k=1
y=j&i // => bit comparison of i with j
sys: logInfo (sys:ui, "y=%d, i=%d, j=%d", y,i,j) // => y=2, i=3, j=2
y=j|k // => higher bit of j or k is written to y
sys: logInfo (sys:ui, "y=%d, j=%d, k=%d", y,j,k) // => y=3, j=2, k=1
y=i|j // => higher bit of i or j is written in y
sys: logInfo (sys:ui, "y=%d, i=%d, j=%d", y,i,j) // => y=3, i=3, j=2
y=i^j // => exclusive or of i and j
sys: logInfo (sys:ui, "y=%d, i=%d, j=%d", y,i,j) // => y=1, i=3, j=2
i=k<<1 // => shift bits of k 1 place to the left
sys: logInfo (sys:ui, "i=%d, k=%d", i,k) // => i=2, k=1
y=i<<j // => shift bits of i j places to the left
sys: logInfo (sys:ui, "y=%d, i=%d, j=%d", y,i,j) // => y=8, i=2, j=2
opDemo(i>>k) // => a=1
sys: logInfo (sys:ui, "i=%d, k=%d", i,k) // => y=11
endprogram
function opDemo(int32a)
sys:logInfo (sys:ui, "a=%d", a) // => a=1
endfunction
Self-Assignment Operators
For self-assignments, the variable in which the result is written is on the side of the expression with the calculation. With standard assignment operators, this results in the following notation:
variable = variable operator expression
The self-assignment operators allow a spelling where the variable is written only once in the calculation:
variable operator = expression
The following table describes the allowed operators for self-assignments and the possible data types for the variables.
| Operator | Numeric Data Types | Text and Buffer |
|---|---|---|
| + | ✔ | ✔ |
| - | ✔ | — |
| * | ✔ | — |
| / | ✔ | — |
| & | ✔ | — |
| | | ✔ | — |
| ^ | ✔ | — |
Example: Self-Assignment Operators
text _msg(20) = "Hello" real64 _x, _y = 3.5714 int32 _i = 2 , _j = 8 _msg += " world!" // => msg = "Hello world!" _x -= _y // => _x = -3.5714 _j| = _i // => _j = 10
Related Information
- Type Conversions
If the target variable has a different data type than a variable that is included in the calculation or assignment, a type conversion is performed.