Operators in C++

An operator is a symbol.

It is used to tells the compiler to perform specific mathematical or logical functions.

C++ language have a following types of built-in operators:−

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

 

Arithmetic Operators

C++ language have a following arithmetic operators.

Assume variable i holds 100 and variable j holds 200 then –

Operator Description Example
+ Adds two operands. i + j = 300
Subtracts second operand from the first. i − j = -100
* Multiplies two operands. i * j = 20000
/ Divide. j / i = 2
% Modulus Operator returns the remainder of integer division. j % i = 0
++ Increment operator increases the value of variable by one. i++ = 101
Decrement operator decreases the value of variable by one. i– = 99

Example : Write a C++ program to demonstrate the various arithmetic operators.

Output

Increment and Decrement Operator

  1. Increment operator      ++
  2. Decrement operator    —

In a C++ language, Increment operator (++)  increases the value of variable by one and Decrement operator  (–) decreases the value of variable by one.

There are two form of increment  and decrement operator:

  1. Prefix form
  2. Postfix form

Prefix Form : In the prefix expression operator appears in the expression before the operands.

Example : ++A 

In the prefix form first the value of operand is increment or decrement than the value of operand is used in expression.

For Example: int  i = 10;
      j = ++i ;    // j=11 , i=11

Postfix Form: In the postfix expression operator appears in the expression after the operands.

Example : A++

In the postfix form first the value of operand is used in expression than value of operand is incremented or decremented.

For Example: int  i = 10;
    j = i++ ;    // j=10, i=11

Example: Write a C++ program to demonstrate the increment and decrement operator. 

Output

Relational Operators

C++ Language has a following relational operators.

For example: Assume variable A holds 100 and variable B holds 200 then −

Operator Description Example
== Equals to (A == B) is not true.
!= Not equal (A != B) is true.
Greater than (A > B) is not true.
Less than (A < B) is true.
>= Greater than or equals to (A >= B) is not true.
<= Less than or equals to (A <= B) is true.

Example:  Write a C++ Program to find the largest of two numbers.

Output

Logical Operators

There are three basic logical operators available in C++ language.

Assume variable A and B holds 1 and 0 respectively then –

Operator Description Example
&&  AND operator (the condition becomes true if both the operands are non-zero) (A && B) is false.
||  OR Operator (the condition becomes true if any of the two operands is non-zero) (A || B) is true.
!  NOT Operator (this operator reverse the logical state of operand. For example: If condition is true, then NOT operator will make it false) !(A && B) is true.

Example:  Write a C++ Program to find the largest among three numbers using AND logical operator.

Output

Bitwise Operators

In a C++ programming language Bitwise operator works on bits and perform bit-by-bit operation.

There are following bitwise operators in C++:-

Operator Description
& Binary AND Operator
| Binary OR Operator
^ Binary XOR Operator
~ Binary One’s Complement Operator is unary
<<  Binary Left Shift Operator
>>  Binary Right Shift Operator

The truth tables for &, |, and ^ is as follows –

a b a & b a | b a ^ b
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

Assume A = 60 and B = 13 in binary format, they will be as follows −

               A in binary     = 0011 1100         (60)                     

              B in binary     = 0000 1101          (13)

                               —————–

Bitwise AND (&)

                      A      = 0011 1100         (60)                

          &         B      = 0000 1101          (13)

———————————————————

                                0000 1100           (12)

Bitwise OR (|)

                      A      = 0011 1100         (60)                

          |          B      = 0000 1101          (13)

———————————————————

                                0011 1101           (61)

Bitwise XOR (^)

                      A      = 0011 1100         (60)                

          ^         B      = 0000 1101          (13)

———————————————————

                                0011 0001           (49)

Binary Left Shift Operator

Left shift operator shift all the bits in a left direction to specified number of times.

Exapmle:

Output:

Output

Exapmle:  Write a program to demonstrate Bitwise operator.

Output

Assignment Operators

In a C++  language there are following assignment operators  −

Operator Description
= assignment operator (it assigns values of right side operands to left side operand )
+= Add assignment operator
-= Subtract assignment operator
*= Multiply  assignment operator
/= Divide assignment operator
%= Modulus assignment operator
<<= Left shift assignment operator
>>= Right shift assignment operator
&= Bitwise AND assignment operator
^= Bitwise exclusive OR assignment operator
|= Bitwise inclusive OR assignment operator

Add Assignment Operator

Example:

Output

Subtract Assignment Operator

Example:

Output

Multiply  assignment operator

Example:

Output

The ?  Operator

This operator also known as conditional operator . It is similar to if-else statement.

Syntax of ? operator

 Expression1 ? Expression 2 : Expression 3;

There are three expressions in ? operator, Expression1, Expression2, and Expression3 .

The value of a ? expression is determined like this −

  • If Exp1 is true, then Exp2 is evaluated.
  • If Exp1 is false, then Exp3 is evaluated.

Example: write a program to find the grater between two number using ? operator.

Output:

Example: write a program to find the grater among three number using ? operator.

Output

Categories C++