# Operators in C++

0
258

C++ provides different types of operators. These operators can be used with variables and literals to get the result. You know some basic arithmetical operators such as +, -, *, / and %. But there are many other operators provided by C++ Programming Language. All operators can be divided in following categories:

• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Other Operators

## Arithmetic Operators

This type of operators are discussed in the article C++ Variable Types. Basic arithmetic operators are +, -, *, /, %. In addition to these operators there are 2 operators to increase and decrease value of the variable. The increment operator is ++ and the decrement operator is . These operators can be used as before the variable (prefix) and after the variable (postfix). For example, you can change the values of the variable in the following way:

``````
int a = 0;
++a; //now "a" is 1
cout << a << endl;
a++; //now "a" is 2
cout << a << endl;
--a; //decrement a. it's 1 now
cout << a << endl;
--a; //decrement a; it is 0 now
cout << a;
cin.ignore();
``````

The output of the program should be the following
1
2
1
0

Below code will explain you that pre-increment first increments the value then assigns the value to the variable. Post increment first assigns the value then increments it.

``````
a = ++b; // a==4, b==4
a = b++; // a==3, b==4
``````

Below code displays the value of varibles using pre and post increment.

``````
int main()
{
int i = 0;
while(i < 10)
{
cout << i++; //post increment
}
cout << endl;
i = 0;
while(i<10)
{
cout << ++i; //pre increment
}
return 0;
}
``````

Below is the output of above program

0123456789
12345678910

## Relational Operators

This category of the operators is used to compare different values. The result of the operation is a Boolean value (true or false). The relational operators are used in the following form Operand1 operator Operand2, where operands can be some variables or literals that are compared. Here is the list of relational operators:

operator
description
==  Equal to  Returns true if the operands are equal. Otherwise false.
!=  Not equal to  Returns true if the operands are not equal. Otherwise false.
<  Less than  Returns true if the operand1 is less than operand2. Otherwise false.
>  Greater than  Returns true if the operand1 is greater than operand2. Otherwise false.
<=  Less than or equal to  Returns true if the operand1 is less than operand2 or equal to operand 2. Otherwise false.
>=  Greater than or equal to  Returns true if the operand1 is greater than operand2 or equal to operand 2. Otherwise false.

Now we can try these operators. Remember, that “cout” outputs 1 for true and 0 for false:

``````
int three = 3;
int five = 5;
cout << " 3 is equal to 5 = " << (three == five) << endl;
cout << " 3 is not equal to 5 = " << (three != five) << endl;
cout << " 3 is less than 5 = " << (three < five) << endl;
cout << " 3 is greater than 5 = " << (three > five) << endl;
cout << " 3 is not less than 5 = " << (three >= five) << endl;
cout << " 3 is not greater than 5 = " << (three <= five) << endl;
``````

The output for above code is:

3 is equal to 5 = 0
3 is not equal to 5 = 1
3 is less than 5 = 1
3 is greater than 5 = 0
3 is not less than 5 = 0
3 is not greater than 5 = 1

## Logical operators

C++ provides 3 operations from Boolean algebra:
and (conjunction) operator `&&`
or (disjunction) operator `||`
not  (negation) operator `!`

The result of Boolean operations for 2 variables x and y is presented in the following tables:

 X Y X && Y X || Y false false false false true false false true false true false true true true true true
 X !X false true true false

Here is an example of the use of Boolean operators:

``````
//initialize x with true
bool x = true;
//initialize y with false
bool y = false;
//now x is x AND y (false)
x = x&&y;
``````

## Bitwise operators

Bitwise operators are similar to the logic operators, but they perform the same logical operations on bits. All data in memory is represented in the binary form. So, variables in form of bits contains only 0 or 1. The following table represents the result of operations for the bitwise operators:

 X Y X & Y X | Y X ^ Y 0 0 0 0 0 0 1 0 1 1 1 1 1 1 0 1 0 0 1 1

C++ provides possibility to use the following bitwise operators:

### Binary AND operator “&”

The resultant bit is set to 1 if and only if both variables have 1 in the corresponding bit. Example of binary &:

``````
10100110 & 00101010 = 00100010
``````

### Binary OR operator “|”

The resultant bit is set to 1 if at least one of the variables has 1 in the corresponding bit. Example of binary |:

``````
10100110 | 00101010 = 10101110
``````

### Binary XOR operator “^”

The result bit is set to 1 if only one of the variables has 1 in the corresponding bit. Example of Binary xor:

``````
10100110 ^ 00101010 =  10001100
``````

### Binary NOT operator “~”

Flips the bits of the variable. For Example:

``````
~10100110 = 01011001
``````

### Binary Left Shift Operator “<< N”

Will shift ‘N’ number of bits to the left. In simple words, N number of bits from the Left will be removed and N number of Zeros will be added to the Right. For Example:

``````
10100110 << 3 = 00110000
``````

### Binary Right Shift Operator “>> N”

Will shift ‘N’ number of bits to the right. In simple words, N number of bits from the Right will be removed and N number of Zeros will be added to the Left. For Example:

``````
10100110 >> 2 = 00101001
``````

## Assignment Operations

The operator “=” is an assignment operator in C++ and it assigns a value to the objects on the left. C++ provides capability to combine assignment operator with almost all the operation we have discussed above and forms a “Composite Assignment Operator”. For example we can add a value to a variable as shown below

``````
int k = 9;
k = k + 7;
//you can add value in a more compact way by combining two operators
//Composite assignment will be
k += 7; // this is equal to k = k + 7
``````

Composite assignment can be represented in a form:

``````
Expression1 ``````

composite-assignment-operator

`````` Expression2
``````

where composite-assignment-operator can be one of the following.

Let X = 10 initially for the below examples

Composite Assignment
Operator
Example
Is Equivalent to
Output
+=  X += 2  X = X + 2  12
-=  X -= 2  X = X – 2  8
*=  X *= 2  X = X * 2  20
/=  X /= 2  X = X / 2  5
%=  X %= 2  X = X % 2  0
<<=  X <<= 2  X = X << 2  40
>>=  X >>= 2  X = X >> 2  2
&=  X &= 2  X = X & 2  2
^=  X ^= 2  X = X ^ 2  8
|=  X |= 2  X = X | 2  10

It is suggested to use Composite Assignment Operator for more efficient code

``````
X +=10; // will be faster than X = X + 10
X = X + 10; // will be slower than X += 10
``````

## Other operators

There are some operators that are not included in any categories listed above, but they are useful and can be used in your programs:

### “sizeof” Operator

sizeof operator returns the size of the variable or type. Here “size” means how many bytes of memory is being used by a variable or type.

``````
cout << sizeof(int) << endl; // will print 4
cout << sizeof(x) << endl; // will print size of x
``````

### Coma (“,”) Operator

Coma “,” Operator is used when you need to perform a sequence of operations. You used it when initialized a list of variable of the same type:

``````
double a = 2.0,
b = 4.3,
c = 2;
``````

## Operators Precedence in C++

Operator precedence determines how an expression is evaluated. Some operators will have higher precedence than others. For example, multiplication operator will have higher precedence than addition operator.

For example a = 2 + 3 * 5; here, a will be assigned 17, not 25 because operator * has higher precedence than +, so 3*5 gets multiplied first and then 2 gets added to the multiplication result of 3*5.

In the table below, operators with the highest precedence appear at the top of the table and operators with the lowest precedence appear at the bottom. In the expression, higher precedence operators will be evaluated first.

Operator
Category
Associativity
Precedence
() [] -> . ++ – – Postfix Left to right
Highest
+ – ! ~ ++ – – (type)* & sizeof Unary Right to left
* / % Multiplicative Left to right
+ – Additive Left to right
<< >> Shift Left to right
< <= > >= Relational Left to right
== != Equality Left to right
& Bitwise AND Left to right
^ Bitwise XOR Left to right
| Bitwise OR Left to right
&& Logical AND Left to right
|| Logical OR Left to right
?: Conditional Right to left
= += -= *= /= %=>>= <<= &= ^= |= Assignment Right to left
, Comma Left to right
Lowest
Previous articleC++ Storage Classes
Next articleTypes of Loops in C++
If you have come this far, it means that you liked what you are reading. I am a software developer (graduated from BITS Pilani). I love writing technical articles on programming and data structures.