Unary Operators
So far, the operators that we've used had a value, typically called an operand, on either side of them: rhs and lhs. Unary operators are those operators, however, that take only one value and modify that. We'll be taking a quick look at minus (-
), increment (++
), and decrement (--
). There are a number of other unary operators (logical complement (!
) and bitwise complement (~
)), but we'll cover these in the following sections.
Let's start with the minus (-
) operator; this allows us to manipulate the sign of a value. It is fairly straightforward—when placed in front of a value, it will turn a negative value positive, and a positive value negative.
Here is an example:
// Negation example. #include <iostream> #include <string> int main() { int myInt = -1; std::cout << -myInt * 5 << std::endl; myInt = 1; std::cout <...