# Operators

Operators are characters or groupings of characters that take some number of variables or literals as operands, apply some specific mathematical, logical, or translational operation, and return a result. Operators are defined by each programming language, although basic ones are often the same or similar. The majority are mathematically binary operators, i.e. they take two operands, though nearly all languages have some unitary operators and a few have operators that take three or more operands. Each operand must be of the specific types for which an operator is valid.

## Arithmetic Operations

Arithmetic operators are defined on integers, floats, and doubles.

+ - add, subtract

* / multiply, divide

Operators are applied in a particular order. This is called precedence. First (equal status): * / Second (equal status): + -

Evaluation is left to right by precedence unless parentheses are used to group operators and operands. The mnemonic PEMDAS is sometimes applied–ParenthesesExponentsMultiplicationDivisionAdditionSubtraction–but remember that MD and AS are equals within their ranking. C++ does not provide an exponential operator so more correctly it would be PMDAS. Exponentiation is supplied by the pow built-in function.

pow(x,3)


Even if the exponent is an integer, pow evaluates it as if both are floating-pint numbers.

### Special Considerations for Integer Operators

In C++ 2/3 is always zero! Why? Because 2 and 3 are both integers, so / is an integer operation that yields an integer result

Exercise: What is 9/5?

The remainder can be obtained with the % operator.

k=n%m


It is defined as $$n-floor(n/m) x m$$ It is mathematically well-defined for negative integers, but the results for such arguments are not generally what most programmers expect.

Example:

#include <iostream>

int main() {

std::cout<<"Mod 11,3 is "<<11%3<<"\n";
std::cout<<"Mod -11,3 is "<<-11%3<<"\n";
std::cout<<"Mod 11,-3 is "<<11%-3<<"\n";

return 0;

}



## Assignment Operators

The simple assignment of a value to a variable is through the equals sign =.

a=b;


C++ supports several compound assignment operators; the first operator specifies the arithmetic or other operation to be performed on the variable on the left-hand side, then the result is assigned back to the same variable.

a+=b;
//Equivalent to
a=a+b;


There is no rule that assignment operators (aside from =) must be used but they save space and typing.

a+=b;
a-=b;
a*=b;
a/=b;
a%=b;


For the special case of adding or subtracting 1, special increment and decrement operators are defined.

++i;
--i;
i++;
i--;


Beware when assigning the result of an increment or decrement to another variable. The “prefix” increment/decrement operators shown here first add or subtract, then change the value of the variable. They are exactly equivalent to i+=1 and i-=1 respectively. The “post” operators i++ and i-- do not change the value of the variable before incrementing or decrementing.

#include <iostream>

int main() {
/* My first program
Author: My Name
Date: Today
*/

int i, j, k;

i=2;

j=i++;

std::cout<<" i is: "<<i<<"\n";
std::cout<<" j is: "<<j<<"\n";

j=++i;
std::cout<<" i is: "<<i<<"\n";
std::cout<<" j is: "<<j<<"\n";

return 0;
}



 i is: 3
j is: 2
i is: 4
j is: 4


Exercise Run the following program. Modify the values of the different variables and see what happens.

#include <iostream>

int main (int argc, char **argv) {
double x,Xs;
int num_1,num_2;

x=17.; Xs=11.;
num_1=10; num_2=14;

std::cout<<x<<"\n";
std::cout<<Xs/x<<"\n";
std::cout<<(int)Xs/x<<"\n";
std::cout<<int(Xs)/int(x)<<"\n";
std::cout<<Xs/x + x<<"\n";
std::cout<<Xs/(x+x)<<"\n";
std::cout<<x/num_1<<"\n";
std::cout<<num_1/num_2<<"\n";
std::cout<<num_2/num_1<<"\n";

return 0;
}



## Conditional Operators

### Comparison Operators

These are used to compare numerical values. They can also compare character and string variables; ordering is determined by the character encoding. They return a Boolean value.

Symbols Meaning
== Equality
!= Not equal
< Less than
<= Less than or equal
> Greater than
>= Greater than or equal to

### Boolean Operators

Operator Meaning
! Negation of what follows
&& and

It is important to note that || is an inclusive or. It evaluates to true if either operand is true. This is different from many human languages, for which “or” is generally, though not always, exclusive. An exclusive “or” is true only if exactly one of the conditions is true. You can have cake or ice cream (but not both). An exclusive or can be constructed with

(a && !b) || ( a && b)


where a and b are Boolean expressions.

“Truth tables” define the outcome of Boolean operators. This example is for “and.”

Operator Operand 1 Operand 2 Result
&& true true true
&& false true false
&& true false false
&& false false false

## Conditional Operator Precedence

Like arithmetic operators, conditional operators have a precedence ordering.

• ! has the highest rank
• >,>=,<,<= are equal and outrank == or !=
• ==,!= are equal and outrank &&
• && outranks ||

Exercise Experiment with different values for the variables in this code.

#include <iostream>

int main() {
double a,b;
int c,n;

a=11.; b=9.; c=45; n=3;

std::cout<< (a>b) << "\n";
std::cout<< (a<b && c==n) << "\n";
std::cout<< (a<b || c==n) << "\n";
std::cout<< (a>b || c==n && a<b) << "\n";
std::cout<< ((a>b || c==n) && a<b) << "\n";
bool is_equal=a==b;
std::cout<< is_equal << "\n";

return 0;
}



## Bitwise Operators

Bitwise operators are defined for “integer type” variables, e.g. int, short i nt, unsigned int, char, unsigned char etc. They return another “integer type” variable.

Operator Meaning
& bitwise and
| bitwise or
^ bitwise xor (exclusive or)
Previous
Next