C++ Operators

C++ Operators are symbols that take one or two operands and perform a specific action on these operands.

Operators can be classified into following groups.

In this tutorial, we shall go through each of these Operators, and also list out individual tutorials that describe respective operators in detail.

Assignment Operators

Arithmetic operators are used to perform common operations like addition, subtraction, multiplication, division, etc.

The following table specifies symbol, example, and description for each of the Assignment Operator in C++.

OperatorSymbolArithmeticOperationExampleDescription
=Simple Assignmentx = 2Assign x with 2.
+=Addition Assignmentx += 3Add 3 to the value of x and assign the result to x.
-=Subtraction Assignmentx -= 3Subtract 3 from x and assign the result to x.
*=Multiplication Assignmentx *= 3Multiply x with 3 and assign the result to x.
/=Division Assignmentx /= 3Divide x with 3 and assign the quotient to x.
%=Remainder Assignmentx %= 3Divide x with 3 and assign the remainder to x.
&=Bitwise AND Assignmentx &= 3Perform x & 3 and assign the result to x.
|=Bitwise OR Assignmentx |= 3Perform x | 3 and assign the result to x.
^=Bitwise-exclusive-OR Assignmentx ^= 3Perform x ^ 3 and assign the result to x.
<<=Left-shift Assignmentx <<= 3Left-shift the value of x by 3 places and assign the result to x.
>>=Right-shift Assignmentx >>= 3Right-shift the value of x by 3 places and assign the result to x.

The following are the list of detailed tutorials for Arithmetic Operators.

Arithmetic Operators

Arithmetic operators are used to perform common operations like addition, subtraction, multiplication, division, etc.

ArithmeticOperatorOperatorSymbolDescriptionExample
Addition+Returns addition of two operands.x + y
SubtractionReturns difference of second operand from first operand.x – y
Multiplication*Returns the product of two operands.x * y
Division/Returns the quotient of division of first operand by second operand.x / y
Modulus%Returns the remainder of division of first operand by second operand.x % y
Increment++Increments the value of variable by 1.++x
DecrementDecrements the value of variable by 1.–x

The following are the list of detailed tutorials for Arithmetic Operators.

Logical Operators

Logical operators are used to perform boolean operations like AND, OR, and NOT.

LogicalOperatorOperatorSymbolExampleDescription
AND&&x && yReturns AND between x and y.
OR||x || yReturns OR between x and y.
NOT!!x Returns negation of x.

The following are the list of detailed tutorials for Logical Operators.

Relational Operators

Relational operators are used to compare given values. These are also called Comparison Operators.

OperatorSymbolRelationalOperationExampleDescription
==Equal tox == yReturns true if x is equal to y, else it returns false.
!=Not equalx != yReturns true if x is not equal to y, else it returns false.
>Greater thanx > yReturns true if x is greater than y, else it returns false.
<Less thanx < yReturns true if x is less than y, else it returns false.
>=Greater than or equal tox >= yReturns true if x is greater than or equal to y, else it returns false.
<=Less than or equal tox <= yReturns true if x is less than or equal to y, else it returns false.

The following are the list of detailed tutorials for Logical Operators.

Bitwise Operators

C++ Bitwise Operators are used to perform bitwise operations on integer or char operands. Bitwise operations are done at bit level, meaning, operations like AND, OR, XOR, etc., are done between respective bits of the operands.

The following table specifies symbol, example, and description for each of the Bitwise Operator in C++.

OperatorSymbolBitwiseOperationExampleDescription
&ANDx & yReturns the bitwise AND operation between x and y.
|ORx | yReturns the bitwise OR operation between x and y.
^XORx ^ yReturns the bitwise XOR operation between x and y.
~Complement~xReturns the complement of x.
<<Left Shiftx << yReturns the result of x left shifted by y number of places.
>>Right Shiftx >> yReturns the result of x right shifted by y number of places.

The following are the list of detailed tutorials for Logical Operators.

Conclusion

In this C++ Tutorial, we learned about different types of operators in C++.