top of page

Operators

 

C++ has inherited a large set of operators from C.

 

In C++ we have the following operators

  • arithmetic operators :  +  , -   *,   ,/   and % 

  • increment  and decrement  operators: ++, --

  • relational operators : <  , <= , > , >= , == , != 

  • logical operators:  || or, && and, !not

  • bitwise operators : & and, | or, ^ exor, << left shift, >> right shift

  • ternary operator:  ?:

  • indirecton and addressof operators:   * ,  &,

  • member of operator :   .

  • assignment operators:  - =, +=, -=, *=, /=, %=, |=, &=, ^=

 

Arithmetic operators

+ will add two numbers and - will subtract numbers. * will multiply the numbers and / will divide the numbers

e.g.

int a = 10;

int b = a+100;//a will be 110

b = a-10;//b will be 0

b = a*10;//b will be 100

b = a/3;//b will be 3

 

Now you may wonder. Why isn't a/3 is 3.33 ? Because b is an int. So the answer is truncated to an integer 3

Then let us use a float number

float c = a/3;//c will still be 3.00

What went wrong again?

This has to do with integer arithmetic.

If in an expression, all operands are integers, then integer arithmetic takes place and answers will be truncated to an integer.

 

If at least one operand is float or double, then there will be "real" arithmetic and you will get correct fractional answer.

float c = (float)a/3; //c will be 3.33

float d= a/3.0;// d will be 3.33

Both the above expressions give correct answer.

In the first case we type-casted a to a float.

 

Converting a variable to another type is called type-casting and is done by writing the new type in parenthesis as prefix.

 

Here we are converting a which is an integer to float.

% is modulo operator which gives remainder when 2 integers are divided.

int d = a%3;//d is 1

Here d will be the remainder when a is divided by 3.

Note : For %  operator, both operands must be of integer type.

Increment/Decrement operators : ++ and --

 

Increment operator ++ works on an integer variable and increments it by 1. And decrement operator -- similarly reduces the value of variable by 1.

Both increment and decrement operators are unary - they take only one operand.

 

They can be used as prefix or suffix to the variable. Such a++ or ++a.

The side effects of ++ and -- vary depending on whether we use these operators as prefix  or suffix (post-increment/decrement)

e.g.

int a = 12;
int b = a++;//a is 13, but b is 12
int c = ++b;//both b and c are 13

Pre-increment operator first increments the variable, and then uses it - as in 3rd statement.

But post increment first uses the variable in the expression and then increments it.

Now let us see example of post-decrement operator.

int a = 12;
int b = a--;//b is 12 and a is 1
1

Ternary operator -   ?:

Ternary operator is a compact way of writing an if expression. It uses 3 operands instead of 2.

  • First operand is a conditional expression.

  • Second operand is the result of the expression if the condition is true .

  • Third is the result if condition is false.

Syntax

  conditional-expression?expr1:expr2

If conditional expression evaluates to true, the result is expr1, else it is expr2

Let us look at an example for ternary operator.

#include<iostream>

using namespace std;

int main()

{

   int m; bool even;

   cin>>m;

   even = m%2==0?true:false;

   cout<<boolalpha<<even;

}

 

Here we are testing if m%2 is equal to 0. If it is zero, even becomes true. Otherwise it is false.

The output of the program is true if input is 12.

The ternary statement above is equivalent to

 

if(m%2==0)

   even = true;

else  

   even = false;

Bit-wise operators

Bit-wise operators allow us to modify the individual bits of a number using low level operations.

 

| - bitwise OR operator logically ORs individual bits of two operands and gives us the result.

int a = 3,b=4;
a = a|b;//a will be 7

a will be 00000011 ORed with 00000100 = 00000111 which will be 7

Similarly & operator ANDs individual bits of two operands and gives us the result.


And ^ operator EXORs individual bits of its operands.

~ (complement) operator toggles individual bits of a number. It gives 1's complement of a number.

Note :

  • & operator gives 1 if both operands are 1

  • | operator gives 1 if either of the operands is 1

  • ^ operator gives 1 if either of the operands, but not both is 1.

  • ~ operator gives 0 if the bit is 1 and gives 1 if the bit is 0.

Assignment operators

 

C++ has an uncommon set of operators called assignment operators.

They work like this.

a+=b is same as a = a+b
sum+= n*n+5 is

sum = sum+n*n+5

There are many such assignment operators in C++. These are

 

+=, -=, *=, /=, %=, &=,|=,^=,<<=,>>=

Exercises:

  1. Which of the following statement increments a number by 1

    • a+1

    • a++

    • ++a

    • all of the above

  2. What is the value of b if a is 10 and b is 12, after the statement b+=a+10; ?

  3. Write a program to read a number and determine if it's a prime number.

  4. Write a line of code to multiply a number by 7, without using multiplication operator.

  5. How do you find if the given number is a power of 2?

  6. Write a program to find the number of bits set in a number.

bottom of page