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 a String to a String. The result is the concatenation of the two strings.

Expression

Result

3 + 5

8

8 + 7.583

15.583

"Auto" + "Policy"

"AutoPolicy"

10 + "5"

"105"

"Room " + 1

"Room 1"

Important: By default, the addition operator does not check for overflow errors. Optionally, you can enable checked arithmetic, which generates Gosu exceptions for overflow errors.

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

9 - 2

7

8 - 3.359

4.641

Important: By default, the subtraction operator does not check for overflow errors. Optionally, you can enable checked arithmetic, which generates Gosu exceptions for overflow errors.

Multiplication operator (*)

The * operator performs arithmetic multiplication, using two numeric values as operands. The result is the same as the input types.

Expression

Result

2 * 6

12

12 * 3.26

39.12

"9" * "3"

27

Important: By default, the multiplication operator does not check for overflow errors. Optionally, you can enable checked arithmetic, which generates Gosu exceptions for overflow errors.

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

10 / 2

5

1 / 0

Infinity

0 / 0

NaN

0/1

0

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

10 % 3

1

2 % 0.75

0.5

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