Operators: Numeric
editOperators: Numeric
editPost Increment
editUse 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.
declare
short i
; storeshort 0
toi
load from
i
→short 0
; promoteshort 0
: resultint
; addint 0
andint 1
→int 1
; implicit castint 1
toshort 1
; storeshort 1
toi
declare
long j
; implicit castint 1
tolong 1
→long 1
; storelong 1
toj
declare
long k
; store defaultlong 0
tok
load from
j
→long 1
; storelong 1
tok
; addlong 1
andlong 1
→long 2
; storelong 2
toj
-
Post increment with the
def
type.
Post Decrement
editUse 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.
declare
short i
; storeshort 0
toi
load from
i
→short 0
; promoteshort 0
: resultint
; subtractint 1
fromint 0
→int -1
; implicit castint -1
toshort -1
; storeshort -1
toi
declare
long j
; implicit castint 1
tolong 1
→long 1
; storelong 1
toj
declare
long k
; store defaultlong 0
tok
load from
j
→long 1
; storelong 1
tok
; subtractlong 1
fromlong 1
→long 0
; storelong 0
toj
-
Post decrement with the
def
type.
Pre Increment
editUse 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.
declare
short i
; storeshort 0
toi
load from
i
→short 0
; promoteshort 0
: resultint
; addint 0
andint 1
→int 1
; implicit castint 1
toshort 1
; storeshort 1
toi
declare
long j
; implicit castint 1
tolong 1
→long 1
; storelong 1
toj
declare
long k
; store defaultlong 0
tok
load from
j
→long 1
; addlong 1
andlong 1
→long 2
; storelong 2
toj
; storelong 2
tok
-
Pre increment with the
def
type.
Pre Decrement
editUse 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_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 decrement with different numeric types.
declare
short i
; storeshort 0
toi
load from
i
→short 0
; promoteshort 0
: resultint
; subtractint 1
fromint 0
→int -1
; implicit castint -1
toshort -1
; storeshort -1
toi
declare
long j
; implicit castint 1
tolong 1
→long 1
; storelong 1
toj
declare
long k
; store defaultlong 0
tok
load from
j
→long 1
; subtractlong 1
fromlong 1
→long 0
; storelong 0
toj
storelong 0
tok
; -
Pre decrement operator with the
def
type.
Unary Positive
editUse 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.
-
Unary positive with the
def
type.
Unary Negative
editUse 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.
-
Unary negative with the
def
type.
Bitwise Not
editUse 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.
-
Bitwise not with the
def
type.
Multiplication
editUse 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.
-
Multiplication with the
def
type.
Division
editUse 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.
-
Division with the
def
type.
Remainder
editUse 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.
-
Remainder with the
def
type.
Addition
editUse 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.
-
Addition with the
def
type.
Subtraction
editUse 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.
-
Subtraction with the
def
type.
Left Shift
editUse 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.
-
Left shift with the
def
type.
Right Shift
editUse 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.
-
Right shift with the
def
type.
Unsigned Right Shift
editUse 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.
-
Unsigned right shift with the
def
type.
Bitwise And
editUse 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.
-
Bitwise and with the
def
type.
Bitwise Xor
editUse 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.
-
Bitwise xor with the
def
type.
Bitwise Or
editUse 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.
-
Bitwise or with the
def
type.