The result of arithmetic operators is double if either operand is double,
else float if either operand is float,
else long if either operand is long, else int. |
n + m | Addition. Eg 7+5 is 12, 3 + 0.14 is 3.14 |
n - m | Subtraction |
n * m | Multiplication. Eg 3 * 6 is 18 |
n / m | Division. Eg 3.0 / 2 is 1.5 , 3 / 2 is 1 |
n % m | Remainder (Mod) after dividing n by m. Eg 7 % 3 is 1 |
++i | Add 1 to i before using the value. |
--i | As above for subtraction |
i++ | Add 1 to i after using the value. |
i-- | As above for subtraction |
The result of all comparisons is 0 (false) or non-zero (true). |
< <= == != > >= |
Logical values are zero (false) and non-zero (true). |
b && c | Conditional "and". true if both operands are true,
otherwise false. Short circuit evaluation.
Eg (false && anything) is false. |
b || c | Conditional "or". true if either operand is true,
otherwise false. Short circuit evaluation.
Eg (true || anything) is true. |
!b | true if b is false, false if b is true. |
b?x:y | if b is true, the value is x, else y.
x and y must be the same type. |
= | Left-hand-side must be an lvalue. |
+= -= *= ... | All binary operators (except && and ||)
can be combined with assignment.
Eg a += 1 is the same as a = a + 1 |
Bitwise operators operate on bits of ints. Result is int. |
i & j | Bits are "anded" - 1 if both bits are 1 |
i | j | Bits are "ored" - 1 if either bit is 1 |
i ^ j | Bits are "xored" - 1 if bits are different |
~i | Bits are complemented (0 -> 1, 1 -> 0) |
i << j | Bits in i are shifted j bits to the left,
zeros inserted on right. |
i >> j | Bits in i are shifted j bits to the right.
On left 0 bits inserted for unsigned, sign bits for signed. |