We find ourselves adding, subtracting, and comparing things on a regular basis. These simple (or complex) operations are just as important in code. In C++, operators perform calculations and compute results. We rely on operators for tasks such as calculating areas and volumes of shapes, comparing values in an if/else statement and checking if a statement is true or false.

## What Is an Operator?

Operators are symbols that are used as shortcuts to perform specific mathematical and logical computations on variables and constants in code. Without operators, C++ developers would have a tough time calculating. There are several types of C++ operators that each perform different operations.

## Arithmetic Operators

These are the C++ operators designed to perform simple math functions in code. For the most part, the symbols and their corresponding uses should not come as a surprise. C++ supports the following arithmetic operators:

Simple, right? Let’s see how this would look in code:

``````#include <iostream>using namespace std;int main () {
int x = 20;
int y = 10;
cout << "x + y is " << x + y << endl;
cout << "x - y is " << x - y << endl;
cout << "x * y is " << x * y << endl;
cout << "x / y is " << x / y << endl;
cout << "x % y is " << x % y << endl;
cout << "x++ is " << x++ << endl;
cout << "x-- is " << x-- << endl;
return 0;
}``````

This code would return the following:

``````x + y is 30
x - y is 10
x * y is 200
x / y is 2
x % y is 0
x++ is 21
x-- is 19``````

These operators can be used for performing calculations on any number of values.

## Assignment Operators

Assignment operators take arithmetic operators in C++ to the next level. These operators allow for the assignment of values to variables. In fact, these simplified operators perform an arithmetic operation on two variables, and the solution assigns the value to the variable on the left. Keep in mind that the values and variables must belong to the same data set.

Here are the most common assignment operators in C++:

Let’s give these variables some values and see how assignment operators work in code:

``````int main () {
int x = 10;
int y = 5;
x += y;
cout<<"x += y is "<<x<<endl;
x -= y;
cout<<"x -= y is "<<x<<endl;
x *= y;
cout<<"x *= y is "<<x<<endl;
x /= y;
cout<<"x /= y is "<<x<<endl;
x %= y;
cout<<"x %= y is "<<x<<endl;
return 0;
}``````

Here’s what the code would return:

``````x += y is 15
x -= y is 5
x *= y is 50
x /= y is 2
x %= y is 0``````

Don’t forget that these values would be assigned to “x” and take the place of the previously assigned value.

## Relational Operators

Next up are relational operators. These C++ operators allow programmers to compare two values. If a comparison is true, the program will return true. Relational operators are often used in “if” statements. If one value is greater than, less than or equal to another value, then perform the function located in the “if” statement. If the comparison is not true, you’ll need to tell the code to output something else.

Below we’ll give you some examples of relational operators as used in C++. See if you can determine what each if/else statement will return.

``````int main(){
int x = 25;
int y = 10;
if (x==y) {
cout<<"x and y are the same"<<endl;
}
else{
cout<<"x and y are not the same"<<endl;
}
if( x != y ){
cout<<"x and y are not the same"<<endl;
}
else{
cout<<"x and y are the same"<<endl;
}
if( x > y ){
cout<<"x is greater than y"<<endl;
}
else{
cout<<"x is not greater than y"<<endl;
}
if( x < y ){
cout<<"x is less than y"<<endl;
}
else{
cout<<"x is not less than y"<<endl;
}
if( x >= y ){
cout<<"x is greater than or equal to y"<<endl;
}
else{
cout<<"x is less than y"<<endl;
}

if( x <= y){
cout<<"x is less than or equal to y"<<endl;
}
else{
cout<<"x is greater than y"<<endl;
}
return 0;
}``````

Can you follow all the logic in the program? Here’s what these if/else statements will return using the values assigned to “x” and “y” above:

``````x and y are not the same
x and y are not the same
x is greater than y
x is not less than y
x is greater than or equal to y
x is greater than y``````

## Logical Operators

We use logical C++ operators to check if statements are true. In their simplest form, logical operators can be used with Boolean variables to determine if a condition is true or not. This is often seen in loops where the program will stay in the loop until the condition returns true or false.

Logical operators can also be used to return true or false in statements with relational, assignment or even arithmetic operators. Let’s first take a look at the three operators and then see how they are used:

By default, these operators will return a 1 if the condition is met and a 0 if it’s not. They can also be used in if/else statements to output different results depending on whether the condition is true.

Let’s put together what we’ve learned thus far and see how logical operators are used in code:

``````int main() {
int x = 5;
int y = 3;
int z = 6;
if (x > y && y < z) {
cout << "x is greater than y and y is less than z"<<endl;  }
else{
cout << "At least one of these comparisons returned false"<<endl;  }
if (x == y || y != z) {    cout << "x is equal to y or y is not equal to z"<<endl;  }
else{
cout << "x is not equal to y and y is equal to z"<<endl;  }  if (!(x -= y)) {    cout << "x minus y is zero"<<endl;  }
else{
cout << "x minus y is not zero"<<endl;  }  return 0;
}``````

The program expertly crunches through the data and evaluates the logical operators. Here’s what it returns for each if/else statement:

``````x is greater than y and y is less than z
x is equal to y or y is not equal to z
x minus y is not zero``````

Some of these examples are certainly more useful than others!

## Bitwise Operators

Bitwise C++ operators perform operations on a bit-by-bit basis, with a bit referring to a single binary digit. These operators take integer-type values, transform them into their binary counterparts and then perform an operation on them. They are typically used for setting, testing and shifting bits.

Let’s take a look at which operators we’re dealing with:

Notice a lot of similarities to the operators we covered above. Be careful not to mix them up or you may throw your code for a loop. Speaking of code, here’s how these operators look:

``````int main() {
int x = 12; // 0000 1100 in binary
int y = 22; // 0001 0110 in binary
cout << "x & y = " << (x & y) << endl;
cout << "x | y = " << (x | y) << endl;
cout << "x ^ y = " << (x ^ y) << endl;
cout << "Inversion of x is " << (~x) << endl;
cout << "Shift bits in x left 2 spaces yields " << (x << 2) << endl;
cout << "Shift bits in x left 1 space yields " << (x >> 1) << endl;

return 0;
}``````

These comparisons and shifts result in the following values:

``````x & y = 4
x | y = 30
x ^ y = 26
Inversion of x is -13
Shift bits in x left 2 spaces yields 48
Shift bits in x left 1 space yields 6``````

It’s worth noting that the value for shifting bits does not need to be a constant. So for example, x << y is a valid use of the shift left operator.

With C++, it’s even possible to have operators work for user-defined classes. In effect, we can use the “+” operator and overload it so that it’s possible to even add two complex objects together. To perform this, we’ll need to rely on the “operator” function. We can overload the operator function with many different operators.

Let’s say we have a class called “Complex.” We’ve used this class to generate three different objects for us: r1, r2 and r3. We can use the operator+ function to add even these three complex objects together.

When we overload “+”:

``operator+(argument_list)``

We can then add together the complex objects located in “Complex” as if they were simple variables:

``r3 = r1 + r2;``

This is just one of the many uses of operator overloading. Again, this can be used with most of the operators we’ve covered today.

## Become a C++ Developer With Udacity

To master C++, you’ll have to get comfortable with C++ operators. While operators can be easy to learn, we’ve only scratched the surface on what operators can accomplish. Our expert-led C++ Nanodegree will not only guide you through operators, but also equip you with all the skills you’ll need to build your own application using C++. Get started today!

Udacity Team