Unary expressions
Gosu supports the following unary (single operand) expressions:
- Numeric negation
- Bit-wise
NOT Typeofexpressions
The following sections describe these expressions.
The value of a typeof
expression cannot be fully determined at compile time. For example, an
expression at compile time might resolve as a supertype. At run time,
the expression may evaluate to a more specific subtype.
Numeric negation
Gosu uses the unary - operator before a number to
indicate numeric negation. The operand must be a number data type. The
result is always the same type as the original number.
Syntax
-value
Examples
Expression |
Result |
|---|---|
|
|
|
|
Bit-wise NOT
The bit-wise NOT operator treats a numeric
value as a series of binary digits (bits) and inverts them. This behavior
is different from the logical NOT
operator (!), which treats
the entire numeral as a single Boolean value. In the following example,
the logical NOT operator
assigns a Boolean value of true
to x if y is false, or false if y is true:
x = !yIn the following example,
the bit-wise NOT operator
(~) treats a numerical
value as a set of bits and inverts each bit, including the sign operator.
z = ~7The decimal number 7 is the
binary value 0111 with a positive sign bit. By using the bit-wise NOT, the expression ~7 evaluates
to the decimal value -8. The binary value 0111 reverses to 1000, the
binary value for 8, and the sign bit changes as well to produce -8.
Use the bit-wise NOT operation to manipulate a
bit mask. A bit mask is a number
or byte field that maintains the state of many items by flags mapping
to each bit in the field.
Typeof expressions
Gosu uses the operator typeof to determine meta information
about the type to which an expression evaluates. The operand can be any
valid data type. The result is the type of the expression.
See also
