Operators: Numeric
Use the post increment operator '++' to INCREASE the value of a numeric type variable/field by 1. An extra implicit cast is necessary to return the promoted numeric type value to the original numeric type value of the variable/field for the following types: byte, short, and char. If a variable/field is read as part of an expression the value is loaded prior to the increment.
Errors
- If the variable/field is a non-numeric type.
Grammar
post_increment: ( variable | field ) '++';
Promotion
| original | promoted | implicit |
|---|---|---|
| byte | int | byte |
| short | int | short |
| char | int | char |
| int | int | |
| long | long | |
| float | float | |
| double | double | |
| def | def |
Examples
Post increment with different numeric types.
short i = 0; i++; long j = 1; long k; k = j++;- declare
short i; storeshort 0toi - load from
i→short 0; promoteshort 0: resultint; addint 0andint 1→int 1; implicit castint 1toshort 1; storeshort 1toi - declare
long j; implicit castint 1tolong 1→long 1; storelong 1toj - declare
long k; store defaultlong 0tok - load from
j→long 1; storelong 1tok; addlong 1andlong 1→long 2; storelong 2toj
- declare
Post increment with the
deftype.def x = 1; x++;- declare
def x; implicit castint 1todef→def; storedeftox - load from
x→def; implicit castdeftoint 1; addint 1andint 1→int 2; implicit castint 2todef; storedeftox
- declare
Use the post decrement operator '--' to DECREASE the value of a numeric type variable/field by 1. An extra implicit cast is necessary to return the promoted numeric type value to the original numeric type value of the variable/field for the following types: byte, short, and char. If a variable/field is read as part of an expression the value is loaded prior to the decrement.
Errors
- If the variable/field is a non-numeric type.
Grammar
post_decrement: ( variable | field ) '--';
Promotion
| original | promoted | implicit |
|---|---|---|
| byte | int | byte |
| short | int | short |
| char | int | char |
| int | int | |
| long | long | |
| float | float | |
| double | double | |
| def | def |
Examples
Post decrement with different numeric types.
short i = 0; i--; long j = 1; long k; k = j--;- declare
short i; storeshort 0toi - load from
i→short 0; promoteshort 0: resultint; subtractint 1fromint 0→int -1; implicit castint -1toshort -1; storeshort -1toi - declare
long j; implicit castint 1tolong 1→long 1; storelong 1toj - declare
long k; store defaultlong 0tok - load from
j→long 1; storelong 1tok; subtractlong 1fromlong 1→long 0; storelong 0toj
- declare
Post decrement with the
deftype.def x = 1; x--;- declare
def x; implicit castint 1todef→def; storedeftox - load from
x→def; implicit castdeftoint 1; subtractint 1fromint 1→int 0; implicit castint 0todef; storedeftox
- declare
Use the pre increment operator '++' to INCREASE the value of a numeric type variable/field by 1. An extra implicit cast is necessary to return the promoted numeric type value to the original numeric type value of the variable/field for the following types: byte, short, and char. If a variable/field is read as part of an expression the value is loaded after the increment.
Errors
- If the variable/field is a non-numeric type.
Grammar
pre_increment: '++' ( variable | field );
Promotion
| original | promoted | implicit |
|---|---|---|
| byte | int | byte |
| short | int | short |
| char | int | char |
| int | int | |
| long | long | |
| float | float | |
| double | double | |
| def | def |
Examples
Pre increment with different numeric types.
short i = 0; ++i; long j = 1; long k; k = ++j;- declare
short i; storeshort 0toi - load from
i→short 0; promoteshort 0: resultint; addint 0andint 1→int 1; implicit castint 1toshort 1; storeshort 1toi - declare
long j; implicit castint 1tolong 1→long 1; storelong 1toj - declare
long k; store defaultlong 0tok - load from
j→long 1; addlong 1andlong 1→long 2; storelong 2toj; storelong 2tok
- declare
Pre increment with the
deftype.def x = 1; ++x;- declare
def x; implicit castint 1todef→def; storedeftox - load from
x→def; implicit castdeftoint 1; addint 1andint 1→int 2; implicit castint 2todef; storedeftox
- declare
Use the pre decrement operator '--' to DECREASE the value of a numeric type variable/field by 1. An extra implicit cast is necessary to return the promoted numeric type value to the original numeric type value of the variable/field for the following types: byte, short, and char. If a variable/field is read as part of an expression the value is loaded after the decrement.
Errors
- If the variable/field is a non-numeric type.
Grammar
pre_decrement: '--' ( variable | field );
Promotion
| original | promoted | implicit |
|---|---|---|
| byte | int | byte |
| short | int | short |
| char | int | char |
| int | int | |
| long | long | |
| float | float | |
| double | double | |
| def | def |
Examples
Pre decrement with different numeric types.
short i = 0; --i; long j = 1; long k; k = --j;- declare
short i; storeshort 0toi - load from
i→short 0; promoteshort 0: resultint; subtractint 1fromint 0→int -1; implicit castint -1toshort -1; storeshort -1toi - declare
long j; implicit castint 1tolong 1→long 1; storelong 1toj - declare
long k; store defaultlong 0tok - load from
j→long 1; subtractlong 1fromlong 1→long 0; storelong 0tojstorelong 0tok;
- declare
Pre decrement operator with the
deftype.def x = 1; --x;- declare
def x; implicit castint 1todef→def; storedeftox - load from
x→def; implicit castdeftoint 1; subtractint 1fromint 1→int 0; implicit castint 0todef; storedeftox
- declare
Use the unary positive operator '+' to the preserve the IDENTITY of a numeric type value.
Errors
- If the value is a non-numeric type.
Grammar
unary_positive: '+' expression;
Examples
Unary positive with different numeric types.
int x = +1; long y = +x;- declare
int x; identityint 1→int 1; storeint 1tox - declare
long y; load fromx→int 1; identityint 1→int 1; implicit castint 1tolong 1→long 1; storelong 1toy
- declare
Unary positive with the
deftype.def z = +1; int i = +z;- declare
def z; identityint 1→int 1; implicit castint 1todef; storedeftoz - declare
int i; load fromz→def; implicit castdeftoint 1; identityint 1→int 1; storeint 1toi;
- declare
Use the unary negative operator '-' to NEGATE a numeric type value.
Errors
- If the value is a non-numeric type.
Grammar
unary_negative: '-' expression;
Examples
Unary negative with different numeric types.
int x = -1; long y = -x;- declare
int x; negateint 1→int -1; storeint -1tox - declare
long y; load fromx→int 1; negateint -1→int 1; implicit castint 1tolong 1→long 1; storelong 1toy
- declare
Unary negative with the
deftype.def z = -1; int i = -z;- declare
def z; negateint 1→int -1; implicit castint -1todef; storedeftoz - declare
int i; load fromz→def; implicit castdeftoint -1; negateint -1→int 1; storeint 1toi;
- declare
Use the bitwise not operator '~' to NOT each bit in an integer type value where a 1-bit is flipped to a resultant 0-bit and a 0-bit is flipped to a resultant 1-bit.
Errors
- If the value is a non-integer type.
Bits
| original | result |
|---|---|
| 1 | 0 |
| 0 | 1 |
Grammar
bitwise_not: '~' expression;
Promotion
| original | promoted |
|---|---|
| byte | int |
| short | int |
| char | int |
| int | int |
| long | long |
| def | def |
Examples
Bitwise not with different numeric types.
byte b = 1; int i = ~b; long l = ~i;- declare
byte x; storebyte 1to b - declare
int i; load fromb→byte 1; implicit castbyte 1toint 1→int 1; bitwise notint 1→int -2; storeint -2toi - declare
long l; load fromi→int -2; implicit castint -2tolong -2→long -2; bitwise notlong -2→long 1; storelong 1tol
- declare
Bitwise not with the
deftype.def d = 1; def e = ~d;- declare
def d; implicit castint 1todef→def; storedeftod; - declare
def e; load fromd→def; implicit castdeftoint 1→int 1; bitwise notint 1→int -2; implicit castint 1todef→def; storedeftoe
- declare
Use the multiplication operator '*' to MULTIPLY together two numeric type values. Rules for resultant overflow and NaN values follow the JVM specification.
Errors
- If either of the values is a non-numeric type.
Grammar
multiplication: expression '*' expression;
Promotion
| byte | short | char | int | long | float | double | def | |
| byte | int | int | int | int | long | float | double | def |
| short | int | int | int | int | long | float | double | def |
| char | int | int | int | int | long | float | double | def |
| int | int | int | int | int | long | float | double | def |
| long | long | long | long | long | long | float | double | def |
| float | float | float | float | float | float | float | double | def |
| double | double | double | double | double | double | double | double | def |
| def | def | def | def | def | def | def | def | def |
Examples
Multiplication with different numeric types.
int i = 5*4; double d = i*7.0;- declare
int i; multiplyint 4byint 5→int 20; storeint 20ini - declare
double d; load fromint i→int 20; promoteint 20anddouble 7.0: resultdouble; implicit castint 20todouble 20.0→double 20.0; multiplydouble 20.0bydouble 7.0→double 140.0; storedouble 140.0tod
- declare
Multiplication with the
deftype.def x = 5*4; def y = x*2;- declare
def x; multiplyint 5byint 4→int 20; implicit castint 20todef→def; storedefinx - declare
def y; load fromx→def; implicit castdeftoint 20; multiplyint 20byint 2→int 40; implicit castint 40todef→def; storedeftoy
- declare
Use the division operator '/' to DIVIDE one numeric type value by another. Rules for NaN values and division by zero follow the JVM specification. Division with integer values drops the remainder of the resultant value.
Errors
- If either of the values is a non-numeric type.
- If a left-hand side integer type value is divided by a right-hand side integer type value of
0.
Grammar
division: expression '/' expression;
Promotion
| byte | short | char | int | long | float | double | def | |
| byte | int | int | int | int | long | float | double | def |
| short | int | int | int | int | long | float | double | def |
| char | int | int | int | int | long | float | double | def |
| int | int | int | int | int | long | float | double | def |
| long | long | long | long | long | long | float | double | def |
| float | float | float | float | float | float | float | double | def |
| double | double | double | double | double | double | double | double | def |
| def | def | def | def | def | def | def | def | def |
Examples
Division with different numeric types.
int i = 29/4; double d = i/7.0;- declare
int i; divideint 29byint 4→int 7; storeint 7ini - declare
double d; load fromint i→int 7; promoteint 7anddouble 7.0: resultdouble; implicit castint 7todouble 7.0→double 7.0; dividedouble 7.0bydouble 7.0→double 1.0; storedouble 1.0tod
- declare
Division with the
deftype.def x = 5/4; def y = x/2;- declare
def x; divideint 5byint 4→int 1; implicit castint 1todef→def; storedefinx - declare
def y; load fromx→def; implicit castdeftoint 1; divideint 1byint 2→int 0; implicit castint 0todef→def; storedeftoy
- declare
Use the remainder operator '%' to calculate the REMAINDER for division between two numeric type values. Rules for NaN values and division by zero follow the JVM specification.
Errors
- If either of the values is a non-numeric type.
Grammar
remainder: expression '%' expression;
Promotion
| byte | short | char | int | long | float | double | def | |
| byte | int | int | int | int | long | float | double | def |
| short | int | int | int | int | long | float | double | def |
| char | int | int | int | int | long | float | double | def |
| int | int | int | int | int | long | float | double | def |
| long | long | long | long | long | long | float | double | def |
| float | float | float | float | float | float | float | double | def |
| double | double | double | double | double | double | double | double | def |
| def | def | def | def | def | def | def | def | def |
Examples
Remainder with different numeric types.
int i = 29%4; double d = i%7.0;- declare
int i; remainderint 29byint 4→int 1; storeint 7ini - declare
double d; load fromint i→int 1; promoteint 1anddouble 7.0: resultdouble; implicit castint 1todouble 1.0→double 1.0; remainderdouble 1.0bydouble 7.0→double 1.0; storedouble 1.0tod
- declare
Remainder with the
deftype.def x = 5%4; def y = x%2;- declare
def x; remainderint 5byint 4→int 1; implicit castint 1todef→def; storedefinx - declare
def y; load fromx→def; implicit castdeftoint 1; remainderint 1byint 2→int 1; implicit castint 1todef→def; storedeftoy
- declare
Use the addition operator '+' to ADD together two numeric type values. Rules for resultant overflow and NaN values follow the JVM specification.
Errors
- If either of the values is a non-numeric type.
Grammar
addition: expression '+' expression;
Promotion
| byte | short | char | int | long | float | double | def | |
| byte | int | int | int | int | long | float | double | def |
| short | int | int | int | int | long | float | double | def |
| char | int | int | int | int | long | float | double | def |
| int | int | int | int | int | long | float | double | def |
| long | long | long | long | long | long | float | double | def |
| float | float | float | float | float | float | float | double | def |
| double | double | double | double | double | double | double | double | def |
| def | def | def | def | def | def | def | def | def |
Examples
Addition operator with different numeric types.
int i = 29+4; double d = i+7.0;- declare
int i; addint 29andint 4→int 33; storeint 33ini - declare
double d; load fromint i→int 33; promoteint 33anddouble 7.0: resultdouble; implicit castint 33todouble 33.0→double 33.0; adddouble 33.0anddouble 7.0→double 40.0; storedouble 40.0tod
- declare
Addition with the
deftype.def x = 5+4; def y = x+2;- declare
def x; addint 5andint 4→int 9; implicit castint 9todef→def; storedefinx - declare
def y; load fromx→def; implicit castdeftoint 9; addint 9andint 2→int 11; implicit castint 11todef→def; storedeftoy
- declare
Use the subtraction operator '-' to SUBTRACT a right-hand side numeric type value from a left-hand side numeric type value. Rules for resultant overflow and NaN values follow the JVM specification.
Errors
- If either of the values is a non-numeric type.
Grammar
subtraction: expression '-' expression;
Promotion
| byte | short | char | int | long | float | double | def | |
| byte | int | int | int | int | long | float | double | def |
| short | int | int | int | int | long | float | double | def |
| char | int | int | int | int | long | float | double | def |
| int | int | int | int | int | long | float | double | def |
| long | long | long | long | long | long | float | double | def |
| float | float | float | float | float | float | float | double | def |
| double | double | double | double | double | double | double | double | def |
| def | def | def | def | def | def | def | def | def |
Examples
Subtraction with different numeric types.
int i = 29-4; double d = i-7.5;- declare
int i; subtractint 4fromint 29→int 25; storeint 25ini - declare
double dload fromint i→int 25; promoteint 25anddouble 7.5: resultdouble; implicit castint 25todouble 25.0→double 25.0; subtractdouble 33.0bydouble 7.5→double 25.5; storedouble 25.5tod
- declare
Subtraction with the
deftype.def x = 5-4; def y = x-2;- declare
def x; subtractint 4andint 5→int 1; implicit castint 1todef→def; storedefinx - declare
def y; load fromx→def; implicit castdeftoint 1; subtractint 2fromint 1→int -1; implicit castint -1todef→def; storedeftoy
- declare
Use the left shift operator '<<' to SHIFT lower order bits to higher order bits in a left-hand side integer type value by the distance specified in a right-hand side integer type value.
Errors
- If either of the values is a non-integer type.
- If the right-hand side value cannot be cast to an int type.
Grammar
left_shift: expression '<<' expression;
Promotion
The left-hand side integer type value is promoted as specified in the table below. The right-hand side integer type value is always implicitly cast to an int type value and truncated to the number of bits of the promoted type value.
| original | promoted |
|---|---|
| byte | int |
| short | int |
| char | int |
| int | int |
| long | long |
| def | def |
Examples
Left shift with different integer types.
int i = 4 << 1; long l = i << 2L;- declare
int i; left shiftint 4byint 1→int 8; storeint 8ini - declare
long lload fromint i→int 8; implicit castlong 2toint 2→int 2; left shiftint 8byint 2→int 32; implicit castint 32tolong 32→long 32; storelong 32tol
- declare
Left shift with the
deftype.def x = 4 << 2; def y = x << 1;- declare
def x; left shiftint 4byint 2→int 16; implicit castint 16todef→def; storedefinx - declare
def y; load fromx→def; implicit castdeftoint 16; left shiftint 16byint 1→int 32; implicit castint 32todef→def; storedeftoy
- declare
Use the right shift operator '>>' to SHIFT higher order bits to lower order bits in a left-hand side integer type value by the distance specified in a right-hand side integer type value. The highest order bit of the left-hand side integer type value is preserved.
Errors
- If either of the values is a non-integer type.
- If the right-hand side value cannot be cast to an int type.
Grammar
right_shift: expression '>>' expression;
Promotion
The left-hand side integer type value is promoted as specified in the table below. The right-hand side integer type value is always implicitly cast to an int type value and truncated to the number of bits of the promoted type value.
| original | promoted |
|---|---|
| byte | int |
| short | int |
| char | int |
| int | int |
| long | long |
| def | def |
Examples
Right shift with different integer types.
int i = 32 >> 1; long l = i >> 2L;- declare
int i; right shiftint 32byint 1→int 16; storeint 16ini - declare
long lload fromint i→int 16; implicit castlong 2toint 2→int 2; right shiftint 16byint 2→int 4; implicit castint 4tolong 4→long 4; storelong 4tol
- declare
Right shift with the
deftype.def x = 16 >> 2; def y = x >> 1;- declare
def x; right shiftint 16byint 2→int 4; implicit castint 4todef→def; storedefinx - declare
def y; load fromx→def; implicit castdeftoint 4; right shiftint 4byint 1→int 2; implicit castint 2todef→def; storedeftoy
- declare
Use the unsigned right shift operator '>>>' to SHIFT higher order bits to lower order bits in a left-hand side integer type value by the distance specified in a right-hand side type integer value. The highest order bit of the left-hand side integer type value is not preserved.
Errors
- If either of the values is a non-integer type.
- If the right-hand side value cannot be cast to an int type.
Grammar
unsigned_right_shift: expression '>>>' expression;
Promotion
The left-hand side integer type value is promoted as specified in the table below. The right-hand side integer type value is always implicitly cast to an int type value and truncated to the number of bits of the promoted type value.
| original | promoted |
|---|---|
| byte | int |
| short | int |
| char | int |
| int | int |
| long | long |
| def | def |
Examples
Unsigned right shift with different integer types.
int i = -1 >>> 29; long l = i >>> 2L;- declare
int i; unsigned right shiftint -1byint 29→int 7; storeint 7ini - declare
long lload fromint i→int 7; implicit castlong 2toint 2→int 2; unsigned right shiftint 7byint 2→int 3; implicit castint 3tolong 3→long 3; storelong 3tol
- declare
Unsigned right shift with the
deftype.def x = 16 >>> 2; def y = x >>> 1;- declare
def x; unsigned right shiftint 16byint 2→int 4; implicit castint 4todef→def; storedefinx - declare
def y; load fromx→def; implicit castdeftoint 4; unsigned right shiftint 4byint 1→int 2; implicit castint 2todef→def; storedeftoy
- declare
Use the bitwise and operator '&' to AND together each bit within two integer type values where if both bits at the same index are 1 the resultant bit is 1 and 0 otherwise.
Errors
- If either of the values is a non-integer type.
Bits
| 1 | 0 | |
| 1 | 1 | 0 |
| 0 | 0 | 0 |
Grammar
bitwise_and: expression '&' expression;
Promotion
| byte | short | char | int | long | def | |
| byte | int | int | int | int | long | def |
| short | int | int | int | int | long | def |
| char | int | int | int | int | long | def |
| int | int | int | int | int | long | def |
| long | long | long | long | long | long | def |
| def | def | def | def | def | def | def |
Examples
Bitwise and with different integer types.
int i = 5 & 6; long l = i & 5L;- declare
int i; bitwise andint 5andint 6→int 4; storeint 4ini - declare
long lload fromint i→int 4; promoteint 4andlong 5: resultlong; implicit castint 4tolong 4→long 4; bitwise andlong 4andlong 5→long 4; storelong 4tol
- declare
Bitwise and with the
deftype.def x = 15 & 6; def y = x & 5;- declare
def x; bitwise andint 15andint 6→int 6; implicit castint 6todef→def; storedefinx - declare
def y; load fromx→def; implicit castdeftoint 6; bitwise andint 6andint 5→int 4; implicit castint 4todef→def; storedeftoy
- declare
Use the bitwise xor operator '^' to XOR together each bit within two integer type values where if one bit is a 1 and the other bit is a 0 at the same index the resultant bit is 1 otherwise the resultant bit is 0.
Errors
- If either of the values is a non-integer type.
Bits
The following table illustrates the resultant bit from the xoring of two bits.
| 1 | 0 | |
| 1 | 0 | 1 |
| 0 | 1 | 0 |
Grammar
bitwise_xor: expression '^' expression;
Promotion
| byte | short | char | int | long | def | |
| byte | int | int | int | int | long | def |
| short | int | int | int | int | long | def |
| char | int | int | int | int | long | def |
| int | int | int | int | int | long | def |
| long | long | long | long | long | long | def |
| def | def | def | def | def | def | def |
Examples
Bitwise xor with different integer types.
int i = 5 ^ 6; long l = i ^ 5L;- declare
int i; bitwise xorint 5andint 6→int 3; storeint 3ini - declare
long lload fromint i→int 4; promoteint 3andlong 5: resultlong; implicit castint 3tolong 3→long 3; bitwise xorlong 3andlong 5→long 6; storelong 6tol
- declare
Bitwise xor with the
deftype.def x = 15 ^ 6; def y = x ^ 5;- declare
def x; bitwise xorint 15andint 6→int 9; implicit castint 9todef→def; storedefinx - declare
def y; load fromx→def; implicit castdeftoint 9; bitwise xorint 9andint 5→int 12; implicit castint 12todef→def; storedeftoy
- declare
Use the bitwise or operator '|' to OR together each bit within two integer type values where if at least one bit is a 1 at the same index the resultant bit is 1 otherwise the resultant bit is 0.
Errors
- If either of the values is a non-integer type.
Bits
The following table illustrates the resultant bit from the oring of two bits.
| 1 | 0 | |
| 1 | 1 | 1 |
| 0 | 1 | 0 |
Grammar
bitwise_or: expression '|' expression;
Promotion
| byte | short | char | int | long | def | |
| byte | int | int | int | int | long | def |
| short | int | int | int | int | long | def |
| char | int | int | int | int | long | def |
| int | int | int | int | int | long | def |
| long | long | long | long | long | long | def |
| def | def | def | def | def | def | def |
Examples
Bitwise or with different integer types.
int i = 5 | 6; long l = i | 8L;- declare
int i; bitwise orint 5andint 6→int 7; storeint 7ini - declare
long lload fromint i→int 7; promoteint 7andlong 8: resultlong; implicit castint 7tolong 7→long 7; bitwise orlong 7andlong 8→long 15; storelong 15tol
- declare
Bitwise or with the
deftype.def x = 5 ^ 6; def y = x ^ 8;- declare
def x; bitwise orint 5andint 6→int 7; implicit castint 7todef→def; storedefinx - declare
def y; load fromx→def; implicit castdeftoint 7; bitwise orint 7andint 8→int 15; implicit castint 15todef→def; storedeftoy
- declare