Operators: Difference between revisions
imported>Heeheehee m →Bitwise Operators: Fixed a few more formatting issues. This'll next be updated when ^ starts meaning XOR in ASH. |
imported>Bale reshuffle order |
||
Line 13: | Line 13: | ||
}} | }} | ||
Note that, with the exception of using <nowiki>"+"</nowiki> for string concatenation, these operators can only be used on int or float datatypes. | Note that, with the exception of using <nowiki>"+"</nowiki> for string concatenation, these operators can only be used on int or float datatypes. | ||
==Bitwise Operators== | |||
The following mathematical operators are used for operating on the bits of integers. The operands must be integers; no coercion is allowed: | |||
{{ | |||
eztable| | |||
{{eztr|{{eztd| <nowiki>&</nowiki> |and|a & b}}}} | |||
{{eztr|{{eztd| <nowiki>|</nowiki> |or|a <nowiki>|</nowiki> b}}}} | |||
{{eztr|{{eztd| ~ |not|~a}}}} | |||
{{eztr|{{eztd| << |left shift|a << b}}}} | |||
{{eztr|{{eztd| >> |right shift|a >> b}}}} | |||
{{eztr|{{eztd| <nowiki>&=</nowiki> |and|<nowiki>a &= b --> a = a & b</nowiki>}}}} | |||
{{eztr|{{eztd| <nowiki>|=</nowiki> |or|<nowiki>a |= b --> a = a | b</nowiki>}}}} | |||
{{eztr|{{eztd| >>> |unsigned right shift|a >>> b}}}} | |||
}} | |||
==Assignment Operators== | ==Assignment Operators== | ||
Line 25: | Line 40: | ||
{{eztr|{{eztd| <nowiki>%=</nowiki> |<nowiki>a = a % b</nowiki>}}}} | {{eztr|{{eztd| <nowiki>%=</nowiki> |<nowiki>a = a % b</nowiki>}}}} | ||
{{eztr|{{eztd| <nowiki>**=</nowiki> |<nowiki>a = a ** b</nowiki>}}}} | {{eztr|{{eztd| <nowiki>**=</nowiki> |<nowiki>a = a ** b</nowiki>}}}} | ||
{{eztr|{{eztd| <nowiki>>>=</nowiki> |<nowiki>a = a >> b</nowiki>}}}} | |||
{{eztr|{{eztd| <nowiki>>>>=</nowiki> |<nowiki>a = a >>> b</nowiki>}}}} | |||
}} | }} | ||
Of these, only += and = are usable for strings. See [[Operators#Mathematical Operators|Mathematical Operators]] for information regarding the basic Mathematical Operators. | Of these, only += and = are usable for strings. See [[Operators#Mathematical Operators|Mathematical Operators]] for information regarding the basic Mathematical Operators. | ||
Line 123: | Line 140: | ||
} | } | ||
</syntaxhighlight>}} | </syntaxhighlight>}} | ||
[[Category:Scripting]] | [[Category:Scripting]] |
Revision as of 22:52, 20 January 2011
Mathematical Operators
The following mathematical operators are used in KoLmafia:
+ | Addition | Performs addition and string concatenation |
- | Subtraction | Performs subtraction |
* | Multiplication | Performs multiplication |
/ | Division | Performs division |
% | Modulo | Returns the remainder after division |
** | Exponent | Performs exponentiation |
Note that, with the exception of using "+" for string concatenation, these operators can only be used on int or float datatypes.
Bitwise Operators
The following mathematical operators are used for operating on the bits of integers. The operands must be integers; no coercion is allowed:
& | and | a & b |
| | or | a | b |
~ | not | ~a |
<< | left shift | a << b |
>> | right shift | a >> b |
&= | and | a &= b --> a = a & b |
|= | or | a |= b --> a = a | b |
>>> | unsigned right shift | a >>> b |
Assignment Operators
The following assignment operators are used in KoLmafia (let a = left operand, b = right operand):
= | a = b |
+= | a = a + b |
-= | a = a - b |
*= | a = a * b |
/= | a = a / b |
%= | a = a % b |
**= | a = a ** b |
>>= | a = a >> b |
>>>= | a = a >>> b |
Of these, only += and = are usable for strings. See Mathematical Operators for information regarding the basic Mathematical Operators.
Relational Operators
To follow these examples, a basic understanding of the concepts found on Control Structures would be helpful.
In order to create more complex if statements, we need to understand the basic relational operators:
== | equal to |
!= | not equal to |
< | less than |
> | greater than |
<= | less than or equal to |
>= | greater than or equal to |
Note that you cannot mix datatypes within a comparison or KoLmafia will abort with an error, with the exception of mixing types int and float, where KoLmafia will do a transparent type conversion behind-the-scenes. If you need to compare different datatypes, use one or more of the Datatype Conversion functions. Also, == is case-insensitive with respect to strings.
if ( true == true )
{
print( "This line DOES get printed." );
}
if ( true == false )
{
print( "This line does NOT get printed." );
}
if ( 1 == 1.0 )
{
print( "This line DOES get printed." );
}
if ( 1 == 2 )
{
print( "This line does NOT get printed." );
}
if ( "Hello" == "hello" )
{
print( "This line DOES get printed." );
}
Boolean Operators
&& | and |
|| | or |
! | not |
Note that the above operators only work with boolean values & datatypes. To make use of them with other datatypes, you will either need to first perform a Datatype Conversion, or you will need to nest your operations such that a boolean value is used with the boolean operators.
if ( true && true )
{
print( "This line DOES get printed (both possibilities proved true)." );
}
if ( true && false )
{
print( "This line does NOT get printed (only one possibility proved true)." );
}
if ( true || false )
{
print( "This line DOES get printed (since at least one of the possibilities proved true)." );
}
if ( ! false )
{
print( "This line DOES get printed (since the not operator converted false to true)." );
}
We also need to understand operator precedence. Statements inside a () pair are always evaluated first, then from left to right.
if ( true || true && false )
{
print( "This line DOES get printed." );
// evaluated left-to-right
// true or (true && false) returns true
}
if ( true && ( true && false ) )
{
print( "This line does NOT get printed." );
// ( true && false ) is evaluated first since it is inside of parentheses
// so we end up evaluating ( true && false ) which returns false
}
if ( true && ! ( true && false ) )
{
print( "This line DOES get printed." );
// ( true && false ) is evaluated first since it is inside of parentheses
// the ! operator converts the false from ( true && false ) to true
// ( true && true ) returns true
}