Binary and unary operators

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.

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!

Assigning values

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, a += 5; is equivalent to a = a + 5;. It will return the new value of the expression on the left.

Comparison operators

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.

Arithmetic operators

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

Int if both e1 and e2 are Int. Float if both are Int or Float (only one needs to be Float), or else it returns String.

e1 - e2

Subtracts the value of e2 from the value of e1.

Int if both e1 and e2 are Int. Returns Float as soon as one is Float.

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 Float.

e1 % e2

Returns the modulo of the value of e1 by the value of e2.

See return type of subtraction.

Boolean operators

Boolean operators compare two Boolean values and return a Boolean:

Operator

Explanation

e1&& e2

Performs the logical AND operation between e1 and e2.

e1 || e2

Performs the logical OR operation between e1 and e2.

Bitwise operators

These operators compare two integers bit to bit and always return an Int:

Operator

Explanation

e1 | e2

Bitwise OR between the value of e1 and the value of e2.

e1& e2

Bitwise AND between the value of e1 and the value of e2.

e1 ^ e2

Bitwise XOR between the value of e1 and the value of e2.

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.

Unary operators

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 Bool.

-e1

Inverse the sign of e1, where e1 is an Int or a Float.

++e1 or --e1

Increases or decreases the value of e1 and returns the value. e1 should be an Int or a Float. (see example below)

e1++ or e1--

Increments or decrements the value of e1 and returns the old value of e1. e1 should be an Int or a Float. (see example below)

~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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.168.214