Arithmetic Operators
Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012
Use arithmetic operators to perform calculations in X++. Except for ~, which is unary, all arithmetic operators are dyadic; that is, they work on two operands. The syntax of arithmetic expressions is: expression1 ArithmeticOperator expression2.
Operator |
Term |
Description |
---|---|---|
<< |
Left shift |
Performs expression2 left shift (a multiplication by 2) on expression1. |
>> |
Right shift |
Performs expression2 right shift (a division by 2) on expression1. |
* |
Multiply |
Multiplies expression1 by expression2. |
/ |
Divide |
Divides expression1 by expression2. |
DIV |
Integer division |
Performs an integer division of expression1 by expression2. |
MOD |
Integer remainder |
Returns the remainder of an integer division of expression1 by expression2. |
~ |
Not |
Unary operator. Performs a binary not-operation. |
& |
Binary AND |
Performs a binary and-operation on expression1 and expression2. |
^ |
Binary XOR |
Performs a binary XOR-operation on expression1 and expression2. |
| |
Binary OR |
Performs a binary or-operation on expression1 and expression2. |
+ |
Plus |
Adds expression1 to expression2. |
- |
Minus |
Subtracts expression2 from expression1. |
? |
Ternary operator |
Takes three expressions: expression1 ? expression2 : expression3. If expression1 is true, expression2 is returned; otherwise, expression3 is returned. |
Examples
Operator |
Example |
Description |
---|---|---|
<< |
i = 1 << 4; |
Performs 4 left shifts on 1 (1*2*2*2*2). This gives: i=16. |
>> |
i = 16 >> 4; |
Performs 4 right shifts on 16 (16/2/2/2/2). This gives i=1. |
* |
i = 4*5; |
Multiplies 4 by 5. i=20. |
/ |
i = 20/5; |
Divides 20 by 5. i=4. |
div |
i = 100 div 21; |
Returns the integer division of 100 by 21. i=4 (4*21 = 84, remainder 16). |
mod |
i = 100 mod 21; |
Returns the remainder of the integer division of 100 by 21. i=16. |
~ |
i = ~1; |
Binary negates 1. This gives i=-2 (all bits are reversed). |
& |
i = 1 & 3; |
Binary AND. Return the bits in common in the two integers. i=1. |
| |
i = 1 | 3; |
Binary OR. Return the bits set in either 1 or 3. i=3. |
^ |
i = 1 ^ 3; |
Binary XOR. Returns the bits set in 1 and NOT in 3 and vice versa. i=2. |
+ |
i = 1 + 3; |
Adds 1 and 3. i=4. |
- |
i = 3 - 1; |
Subtracts 1 from 3. i=2. |
? |
i = (400>4) ? 1 : 5; |
If (400>4) 1 is returned, else 5 is returned. As 400>4, 1 is returned. i=1. |
See also
Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.