Facebook Page Twitter Page LinkedIn Page
× C++ Tutorial C++ Advanced C++ Resources


Operators are used to perform operation on a single operand or two operands. Operators in C++ can be categorized as follows:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • Bitwise operators
  • Other operators

Arithmetic operators

Arithmetic operators are used to perform arithmetic operations on two operands.

OperatorNameDescription
+AdditionAdd two values
-SubtractionSubtract one value from another
*MultiplicationMultiply two values
/DivisionDivide one value by another
%ModulusReturns remainder of division operation

Example

Assignment operators

Assignment operators are used to assign values of right hand side expression to left hand side operand.

OperatorExpressionEquivalent toDescription
=a = 5a = 5Example
+=a += ba = a + b
-=a -= ba = a - b
*=a *= ba = a * b
/=a /= ba = a / b
%=a %= ba = a % b
&=a &= ba = a & bMore Info
|=a |= b a = a | bMore Info
^=a ^= b a = a ^ bMore Info
>>=a >>= ba = a >> bMore Info
<<=a <<= ba = a << bMore Info

Comparison operators

Comparison operators are used to compare values of two operands. It returns true when values matches and false when values doen not match.

OperatorDescription
==Equal
!= Not equal
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Example

Increment/Decrement operators

Increment and decrement operators are used to increase and decrease the value of variable.

OperatorNameDescriptionExample
++xPre-incrementIncreases the value of x by 1, then returns x.Example
x++Post-incrementReturns x, then increases the value of x by 1.
--xPre-decrementDecreases the value of x by 1, then returns x.Example
x--Post-decrementReturns x, then decreases the value of x by 1.

Logical operators

Logical operators are used to combine two or more conditions.

Operator Name Description
&&ANDReturns True when all conditions are true
||ORReturns True when any of the conditions is true
!NOTReturns opposite boolean result

More Info

Bitwise operators

Bitwise operators are used to perform bitwise operations on two operands.

OperatorNameDescriptionMore Info
&ANDReturns 1 if both bits at the same position in both operands are 1, else returns 0.More Info
|ORReturns 1 if one of two bits at the same position in both operands is 1, else returns 0.More Info
^XORReturns 1 if only one of two bits at the same position in both operands is 1, else returns 0.More Info
~NOTReverse all the bits.More Info
>>Right shiftThe left operand is moved right by the number of bits present in the right operand.More Info
<<Left shiftThe left operand value is moved left by the number of bits present in the right operand.More Info

Other operators

The below table describes other operators supported by C++:

OperatorDescription
sizeof()Returns size of a data type, constants or variable.
ternary operator (?:)Returns one of the two values based on value of boolean expression.

5