# C++ Operators

In this tutorial, you will learn about the different types of operators in C++ with the help of examples.

An operator in C++ is a symbol in the code which instructs the compiler to perform a specific operation on either one or more operands. C++ is rich in operators and they also have their own level of precedence. The different types of Operators available in C++ are as follows :

• Arithmetic Operator
• Relational Operator
• Logical Operator
• Bitwise Operator
• Assignment Operator
• Misc Operator

We will discuss all the operators one by one and discuss their functionalities.

## Arithmetic Operator

Arithmetic Operators either use two or one operands and return a value as the result. C++ supports the following 7 types of Arithmetic operators and let us assume we take two variables where: int a=10,b=20; Then the following operators would output the following.

 Operator Description Example + It is the addition operator which returns the sum of two variables ‘a+b’ would give 30 – It is the subtraction operator which subtracts the second operand from the first ‘a-b’ would give -10. * It is the multiplication operator which multiplies both the operands ‘a*b’ would give 200 / It is the division operator which divides the numerator by the denominator ‘b/a’ would give 2 % It is the modulus operator that gives the remainder of a division as the result ‘a%b’ would give 0 ++ It is the increment operator which increases the current value of a variable by 1 ‘a++’ would give 11 — It is the decrement operator which decreases the current value of a variable by 1 ’a–’ would give 9

## Relational Operator

Relational Operators use two operators and return a Boolean value depending on the values of the two variables. In C++ there are 6 relational operators where we assume two variables are int a=10,b=20; Then the following operators will return the following result.

 Operator Description Example == This operator returns true if both the operands do not have the same value. ‘a==b’ would return False. != This operator returns true if both the operands have the same value. ‘a!=b’ would return True. > This operator returns true if the first value is greater than the second value. ‘a>b’ would return False. < This operator returns true if the first value is smaller than the second value. ‘a= This operator returns true if the first value is greater or equal to the second value. ‘a>=b’ would return False. <= This operator returns true if the first value is smaller than the second value. ‘a<=b’ would return True.

## Logical Operator

Logical operators work on two different Boolean values. They are true and false which are denoted by ‘1’ and ‘0’. They operate on two different or one Boolean variable and return a Boolean value. Let us assume two Boolean variables which hold true and false value. Let us assume bool a=true,b=false; and then the following results would output :

 Operator Description Example && This is the Logical AND operator which returns true only when both the operands are true. ‘a&&b’ would give false as the result. || This is the Logical OR operator which returns true only when either of the operands is true. ‘a||b’ would return true as the result. ! This is the logical NOT operator which returns true only when the Boolean variable holds the value of false. It negates the Boolean value hold by a Boolean variable. ‘!a’ would give false as the result.

## Bitwise Operator

Bitwise operator works bit by bit on the operand and there are 3 types of the bitwise operator which work on two operands or one. Let us assume two binary values of a and b which store the following values.

a=0011 1001 or 57 in decimal

b=0000 1100 or 12 in decimal

The truth table for some of the basic operations are as follows:

 X Y X&Y X|Y X^Y ~X X<<1 X>>1 0 0 0 0 0 1 00 0 0 1 0 1 1 1 00 0 1 0 0 1 1 0 10 0 1 1 1 1 0 0 10 0

 Operator Description Example & Bitwise AND returns 1 only when both the operands have 1 and both the values are compared bit by bit. ‘a&b’ would give ‘00001000’ as the result or 8 in decimal. | Bitwise OR returns 1 only when either of the operands has 1 and both the values are compared bit by bit. ‘a|b’ would give ‘00111101’ as the result or 61 in decimal. ^ Bitwise XOR returns 1 only when either of the operands have 1 or both have different values. If both of the bits are 1 or 0, it would result in 0 or else 1. ‘a^b’ would give ‘00110101’ as the result or 53 in decimal. ~ Bitwise NOT returns 1 only when the operand has a value of 0. The value would return 0 when the operand value is 1. ‘~a’ would return ‘11000110’ as the result or -70 in decimal. << Bitwise Left shift operator shifts the left operand of the value by left by as many decimals mentioned by the second operand. ‘b<<2’ would give ‘00110000’ as the result or 48 in decimal. >> Bitwise Left shift operator shifts the left operand of the value by right by as many decimals mentioned by the second operand. ‘b>>2’ would give ‘00000011’ as the result or 48 in decimal.

## Assignment Operator

Assignment Operators are operators which assign values from the right side operands to the left side operands. There are 11 types of different types of operators in C++. The following values result into :

 Operator Description Example = The simple assignment operator assigns the value on the right-hand side of the operator to the left-hand side. ‘C=A+B’ will assign the value of ‘A+B’ into ‘C’ += The Addition and assignment operator together adds the right side operand to the left operand and then assign the result to the left operand ‘C+=A’ will assign the value of ‘C+A’ into ‘C’ -= The Subtraction and assignment operator together subtracts the right side operand from the left operand and then assigns the result to the left operand. ‘C-=A’ will assign the value of ‘C-A’ into ‘C’ *= The Multiplication and assignment operator together multiplies the right side operand to the left operand and then assign the result to the left operand ‘C*=A’ will assign the value of ‘C*A’ into ‘C’ /= The Division and assignment operator together divides the right side operand to the left operand and then assigns the result to the left operand. ‘C/=A’ will assign the value of ‘C/A’ into ‘C’ %= The Modulus and assignment operator together adds the right side operand to the left operand and then assign the result to the left operand. ‘C%=A’ will assign the value of ‘C%A’ into ‘C’ <<= The Left shift and assignment operator together shifts the left side operand by the value on the right and then assign the result to the left operand. ‘C<<=2’ will assign the value of ‘C<<2’ into ‘C’ >>= The Right shift and assignment operator together shifts the left side operand by the value on the right and then assign the result to the left operand. ‘C>>=2’ will assign the value of ‘C>>2’ into ‘C’ &= The Bitwise AND and assignment operator together performs the Bitwise AND operation on the left side operand by taking the value on the right as the second operand and then assign the result to the left operand. ‘C&=D’ will assign the value of ‘C&D’ into ‘C’ ^= The Bitwise XOR and assignment operator together perform the Bitwise XOR operation on the left side operand by taking the value on the right as the second operand and then assign the result to the left operand. ‘C^=D’ will assign the value of ‘C^D’ into ‘C’ |= The Bitwise OR and assignment operator together perform the Bitwise OR operation on the left side operand by taking the value on the right as the second operand and then assign the result to the left operand. ‘C|=D’ will assign the value of ‘C|D’ into ‘C’

## Miscellaneous Operator

There are some other C++ operators which are helpful in day-to-day programs.

 Operator Description sizeof() This operator returns the size of the variable. Eg. sizeof(bool) would return 1 for a Boolean variable Condition ? X : Y The conditional operator is a single line operator which executes the condition first and then executes the statement X when true and Y when false. Comma (,) This operator allows a sequence of operations to be performed. The final value of the whole comma expression is the last expression’s value of the whole comma-separated list. Casting operators This type of operators can be used to convert one type of data explicitly into another type. Eg. ‘int(2.2)’ will return 2 as the result. & and * pointer operators The pointer operators are used for pointer manipulation in C++. The ‘&’ pointer operator will return the address of a variable. The ‘*’ pointer operator will return the pointer to the variable.

## Precedence of Operators in C++

In a group of expressions with multiple operators, a specific precedence order is followed among the operators and that can affect how an expression can get evaluated. The following table shows the precedence of operators. Higher precedence operators get a higher priority over others.

 Category Operator Operator Associativity Postfix () [] -> . Left to Right Unary + – ! ~ ++ — (type*) sizeof 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