Arithmetic expression types
Addition and concatenation operator
(+)
The + operator performs arithmetic addition or string concatenation using
either two numeric data types or two String data types as operands. If you
add two numeric types, the result is numeric. If you add two String
objects, the result is a String. Note the following:
- If both operands are numeric, the
+operator performs addition on numeric types. - If either operand is a
String, Gosu converts the operand that is not aStringto aString. The result is the concatenation of the two strings.
|
Expression |
Result |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Subtraction operator (-)
The - operator performs arithmetic subtraction, using two numeric values
as operands. The result is the same type as the input types.
|
Expression |
Result |
|---|---|
|
|
|
|
|
|
Multiplication operator (*)
The * operator performs arithmetic multiplication, using two numeric
values as operands. The result is the same as the input types.
|
Expression |
Result |
|---|---|
|
|
|
|
|
|
|
|
|
Division operator (/)
The / operator performs arithmetic division using two numeric values as
operands. The result is the same as the input types. The result of floating-point division
follows the specification of IEEE arithmetic.
|
Expression |
Result |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Arithmetic modulo operator (%)
The % operator performs arithmetic modulo operations, using
numeric values as operands. The result is the same type as the input types. The result of a
modulo operation is the remainder if the numerator divides by the denominator.
|
Expression |
Result |
|---|---|
|
|
|
|
|
|
Bitwise AND (&)
The & operator performs a binary bitwise
AND operation on the value on the left side of the operator and the value
on the right side of the operator.
For example, 10 & 15 evaluates to 10. The decimal number 10 is 1010
binary. The decimal number 15 is 1111 binary. In binary, this expression does a bitwise
AND between value 1010 and 1111. The result is binary 1010, which is
decimal 10.
In contrast, 10 & 13 evaluates to 8. The decimal number 10 is 1010
binary. The decimal number 13 is 1101 binary. In binary, this expression does a bitwise
AND between value 1010 and 1101. The result is binary 1000, which is
decimal 8.
Bitwise inclusive OR (|)
The | (pipe character) operator performs a binary bitwise
inclusive OR operation with the value on each side of the operator.
For example, 10 | 15 evaluates to 15. The decimal number 10 is 1010
binary. The decimal number 15 is 1111 binary. In binary, this code does a binary bitwise
inclusive OR with value 1010 and 1111. The result is binary 1111, which is decimal 15.
The expression 10 | 3 evaluates to 11. The decimal number 10 is 1010
binary. The decimal number 3 is 0011 binary. In binary, this does a bitwise OR between value
1010 and 0011. The result is binary 1011, which is decimal 11.
Bitwise exclusive OR
(^)
The ^ (the caret character) operator performs a binary
bitwise exclusive OR operation with the values on both sides of the
operator.
For example, 10 ^ 15 evaluates to 5. The decimal number 10 is 1010 binary.
The decimal number 15 is 1111 binary. In binary, this code does a binary bitwise exclusive
OR with value 1010 and 1111. The result is binary 0101, which is decimal 5.
Bitwise left shift (<<)
The << operator performs a binary bitwise left shift
with the value on the left side of the operator and value on the right side of the operator.
For example, 10 << 1 evaluates to 20. The decimal number 10 is 01010
binary. In binary, this code does a binary bitwise left shift of 01010 one bit to the left.
The result is binary 10100, which is decimal 20.
The expression 10 << 2 evaluates to 40. The decimal number 10 is
001010 binary. In binary, this code does a binary bitwise left shift of 001010 one bit to
the left. The result is binary 101000, which is decimal 40.
Bitwise right shift and preserve sign (>>)
The >> operator performs a binary bitwise right shift
with the value on the left side of the operator and value on the right side of the operator.
For signed values, the >> operator sets the high-order bit with its
previous value for each shift. This preserves the sign (positive or negative) of the result.
For signed integer values, this is the usually the appropriate behavior. Contrast this
behavior with the >>> operator.
For example, 10 >> 1 evaluates to 5. The decimal number 10 is 1010
binary. In binary, this code does a binary bitwise right shift of 1010 one bit to the right.
The result is binary 0101, which is decimal 5.
The expression -10 >> 2 evaluates to -3. The decimal number -10 is
11111111 11111111 11111111 11110110 binary. This expression does a binary bitwise right
shift two bits to the right, filling in the top two bits with a 1 because the original
number was negative. The result is binary 11111111 11111111 11111111 11111101, which is
decimal -3.
Bitwise right shift and clear sign (>>>)
The >>> operator performs a binary bitwise right
shift with the values on both sides of the operator. The >>>
operator sets the high-order bit for each shift to zero. For unsigned integer values, this
behavior is the usually the desirable one. Contrast this behavior with the
>> operator.
For example, 10 >>> 1 evaluates to 5. The decimal number 10 is
1010 binary. In binary, this code does a binary bitwise right shift of 1010 one bit to the
right. The highest value bit is set to 0. The result is binary 0101, which is decimal 5.
The expression -10 >>> 2 evaluates to 1,073,741,821. The decimal
number -10 is 11111111 11111111 11111111 11110110 binary. This expression does a binary
bitwise right shift two bits to the right, filling in the top two bits with a 0. The result
is binary 00111111 11111111 11111111 11111101, which is decimal 1,073,741,821.
See also
