Binary and unary operators are two very important concepts in programming because they can both respectively be used to manipulate data. So, let's start with binary operators.
There are several operators, some of which you may already be familiar with, and some that you may not know, even if you have some programming experience. So, take a look at see all of them!
There are several operators that can assign a value to an expression:
Operator |
Explanation |
---|---|
e1 = e2 |
Assigns the value of e2 to the expression e1. It returns the value of e2; |
+= -+ *= /= %= &= |= ^= <<= >>= >>>= |
Assigns the value to the expression on the left after performing the operation (see before). For example, |
There are several comparison operators, all of them returning either true or false.
Operator |
Explanation |
---|---|
e1 == e2 |
Returns true if e1 and e2 are equal. |
e1 != e2 |
Returns false if e1 and e2 are equal. |
e1> e2 or e1 < e2 or e1 >=e2 or e1 >=e2 |
Returns true if the comparison is true. |
Here are haXe's arithmetic operators; beware of their return type!
Operator |
Explanation |
Return type |
---|---|---|
e1 + e2 |
Adds the value of e1 to the value of e2 |
|
e1 - e2 |
Subtracts the value of e2 from the value of e1. |
|
e1 * e2 |
Multiplies the value of e1 with the value of e2. |
See return type of subtraction. |
e1 / e2 |
Divides the value of e1 by the value of e2. |
Always returns |
e1 % e2 |
Returns the modulo of the value of e1 by the value of e2. |
See return type of subtraction. |
Boolean operators compare two Boolean values and return a Boolean:
Operator |
Explanation |
---|---|
e1&& e2 |
Performs the logical |
e1 || e2 |
Performs the logical |
These operators compare two integers bit to bit and always return an Int
:
Operator |
Explanation |
---|---|
e1 | e2 |
Bitwise |
e1& e2 |
Bitwise |
e1 ^ e2 |
Bitwise |
e1>> e2 |
Performs an arithmetic right shift on the value of e1 by the value of e2. |
e1<< e2 |
Performs an arithmetic left shift on the value of e1 by the value of e2. |
e1>>> e2 |
Performs a logical right shift on the value of e1 by the value of e2. |
There are a few unary operators in haXe and most of them are pretty easy to read. Here they are as follows:
Operator |
Explanation |
---|---|
!e1 |
Inverse the value of e1, where e1 is a |
-e1 |
Inverse the sign of e1, where e1 is an |
++e1 or --e1 |
Increases or decreases the value of e1 and returns the value. e1 should be an |
e1++ or e1-- |
Increments or decrements the value of e1 and returns the old value of e1. e1 should be an |
~e1 |
Returns the one's complement of the value of e1. Be warned that it will produce unpredictable results on Neko because Neko's integers are 31 bits instead of being 32 bits. |
That's it! We've finished with operators. There are some subtleties, such as pre and post incrementing.
The following is an example of increments:
class TesthaXe { public static function main() { var i : Int = 0; var j : Int = 0; trace(i++); trace(++j); } }
This will output the following:
TesthaXe.hx:8: 0
TesthaXe.hx:9: 1
This is because with i++
, the value of i
is returned before incrementing it, while in ++j
, the value is incremented before returning it.
3.15.168.214