Expressions

We can do calculations on our variables and constants by using operators such as plus and minus:

int just_a_little_more = a_little + 1;

We use the operators * and / for multiplication and division:

int pressure = psi / (width * height);

We grouped the last two variables in parentheses to indicate that the multiplication should be done first, then the division. Otherwise the result would have been the same as (psi / width) * height.

Multiplication and division are done before addition and subtraction:

int price = cost_of_cup + cost_of_coffee * ounces_of_coffee;

The other arithmetic operators are the unary minus sign, used to negate a number or variable (such as -1, -price, or -(tax+license_fee)), the modulo operator % (which is the remainder after division, so 5 % 3 is 2), and the increment and decrement operators ++ and --, used to add or subtract 1 from a variable. These last two operators change the variable they are used on. If they are used before a variable, they change the variable before its value is used in the rest of the expression, so in the bit of code:

int seconds = 0;

time = ++seconds + 13;

the value of time would be 14. If the code looked like this:

int seconds = 0;

time = seconds++ + 13;

then the value would be 13. In both cases, the value of seconds after the last statement would be 1.

The C++ language has a large number of operators for constructing expressions, and we have seen examples earlier of the 'shift' operators >> and <<, the 'and' operator &, the 'or' operator | and the exclusive-or operator ^. All of these operators have their 'precedence' much as multiplication happens before addition. But if you string together a lot of operators, my suggestion is to use parentheses to group them rather than trying to remember all of the precedence rules. Your code will then be readable by people who have forgotten whether shifts happen before exclusive-or.

To compare values, we have the relation operators. These are ==, !=, >, <, >=, <=, for equal to, not equal to, less than, greater than, greater than or equal to, and less than or equal to. These are most often used in 'if' statements and loop controls, but they can also be used as arithmetic operators, evaluating to 1 for true, and 0 for false.

Likewise mostly used in control flow statements are the logical operators !, &&, and ||, for NOT, AND, and OR. An interesting feature of the || operator is that if the first operand is true, the expression after the || is ignored, since it will not change the value. This is true even if it has some effect, such as incrementing, assigning, or calling a function. The same is true of the && operator if the first operand is false.

The bitwise operators are ~, &, |, ^, <<, and >>, for complement, AND, OR, XOR, left-shift, and right shift.

The complement of a number is where each bit is the opposite of what it used to be:

int pins = 0b11010;

int not_pins = ~pins;

The value of not_pins is 0b101.

When you use the & operator, the result has a 1 bit only if the corresponding bits in each operand are 1. Thus in:

int pins = 0b11010;

int not_pins = pins & 0b111;

the variable not_pins would have the value 0b10.

When you use the | operator, the result is a 1 bit if either of the corresponding bits are 1:

int pins = 0b11010;

int not_pins = pins | 0b111;

and not_pins ends up with the value 0b11111.

When you use the ^ operator (exclusive or), the result is a 1 bit if one or the other corresponding bits is 1, but not both (the the 'exclusive' adjective):

int pins = 0b11010;

int not_pins = pins ^ 0b111;

and not_pins ends up with the value 0b11101.

The shift operators move all the bits left or right by the amount given in the second operand:

int pins = 0b11010;

int not_pins = pins >> 2;

gives the value 0b110 to the variable not_pins.

For the operators that take two operands, we can simplify assignments by combining the operator with an equal sign. Thus these two statements result in the same action:

a = a + c;

a += c;