Operators in C++

Operators in C++

 

Here, in this page we will discuss about different operators in C++.
Definition: An operator is a special symbol that performs some action on operands.

  • C++ supports a rich set of operators
  • Operators say the compiler to perform mathematical and logical computations on the data stored in memory

Example:
Expression : a+b
a,b are  operands and + is operator

operators in C++

Arithmetic Operators

  • addition(+)
  • subtraction(-)
  • multiplication(*)
  • division(/): returns quotient when a is divided b
  • modulus(%): returns remainder when a is divided by b

Example :

main()
{
int a=3,b=2;
cout<<"\na+b:"<<a+b;
cout<<"\na*b:"<<a*b;
cout<<"\na/b:"<<a/b;//quotient when 3/2 is done
cout<<"\na-b:"<<a-b;
cout<<"\na%b:"<<a%b;//remainder when 3/2 is done
cout<<"\n7.5/3:"<<(float)7.5/3;//default constants are int so typecast to get decimal also
cout<<"\n5*4*3:"<<5*4*3;
}

O/P

a+b:5
a*b:6
a/b:1
a-b:1
a%b:1
7.5/3:2.5
5*4*3=60

Note:

  • ‘%’ operator cannot be applied for float types i.e either numerator or denominator cannot be in float type
  • To solve this we can make use of fmod(op1,op2) which returns remainders for the division of two floating types
main()
{// cout<<3.5%3;//error:invalid for types for %
cout<<fmod(3.5,3);//gives 0.5 as reaminder
}

O/P

0.5
  • ‘%‘ ooperator returns -ve value if either denoimintor or numerator or both are -ve
  • ‘/’ operator returns +ve value if both are -ve and -ve value if either denominator or numerator is -ve
cout<<50/-5;//-10
cout<<-50/-5;//10
cout<<-50/5;//-10

Relational Operators

  • The output of the relational operators is always in the form of 0(false) and 1(true)
  • If the condition is satisfied gives 1 and if the condition is false it gives 0

Operator

Name

Example

Output

Less than

5>2

1

Greater than

5<2

0

==

Equality comparison

5==5

1

>=

Less than or equal to

5>=5

1

<=

Greater than or equal to

6<=5

0

!=

Not equal to

5!=5

0

C++ program demonstrating Relational operators

main()
{
cout<<"\n"<<(5<4);//0
cout<<"\n"<<(5>4);//1
cout<<"\n"<<(5<5);//0
cout<<"\n"<<(5<=5);//1
cout<<"\n"<<(5==4);//0
cout<<"\n"<<(5!=4);//1
}
  • Difference between > and >= is 5>=5 is true and 5>5 is false
  • In the case of 5! =4, as 5 is not equal to 4 gives 1 and 5!=5 gives 0 because 5 is equal to 5
  • 5<4 gives 0 because 4 is not greater than 5 and 5>4 gives 1 as 5 is greater than 4
Competitive Coding Techniques

Logical Operators

  • The output of the logical operators is always in the form of 0 (false) and 1 (true)
  • A logical operator is a valid combination of values,varibales&relational expressions

Form
Input1(logical-operator)Input2

  • Logical AND(&&): It returns true only if both the inputs are true otherwise false
  • Logical OR(||): It returns true if either of the input is true otherwise false
  • Logical NOT(!): It returns true if the input is false and false if input as true

Example:

  • (5>4)&&(4>3) =1&&1=1
  • (5>4)||(5>10)=1||0=1
  • !(5>3)=!(1)=0

Note

In case of logical operators any number is considered as true ,other than zero

  • (-1)&&1=1&&1=1
  • 0&&1=0
  • 5||4=1||1=1
  • !4=0
  • !0=1
  • !-1=0

C++ program to demonstrate Logical Operators

main()
{
cout<<(5&&0);//0
cout<<!(5&&0);//1
cout<<(6||0);//1
cout<<(!false);//1
cout<<(5>4)&&(4>3);//1
cout<<(5>4)||(5>10);//1
cout<<!(5>3);//0

}

Increment/Decrement operators

The ++ and — operators adds 1 and subtract 1 from the existing value at memory location

Pre increment/decrement

Here both expression and value at memory gets updated i.e both ++variable and variable changes

Case 1

main()
{
int a=5;
cout<<"++a:"<<++a<<"\ta:"<<a;//a=a+1
//now a becomes 6 and ++a is also 6
}

O/P

++a:6 a:6

Case 2

main()
{
int a=5;
cout<<"--a:"<<--a<<"\ta:"<<a;
//now a becomes 4 and --a is also 4
}

O/P

++a:5 a:6

Post increment/decrement

Here only value at memory gets updated expression won’t change i.e ++variable and variable changes

Case 3

main()
{
int a=5;
cout<<"a++:"<<a++<<"\ta:"<<a;
//now a becomes 6 but printing a++ gives 5 itself
}

O/P

a++:5 a:6

Case 4

main()
{
int a=5;
cout<<"a--:"<<a--<<"\ta:"<<a;//now a becomes 4 but printing a-- gives 5 itself

}

O/P

a--:5 a:4

Cleary understand the diffeernce between pre and post inc/dec operators

Increment /decrement operators at wherein the program may update the value at the memory and continue with the updated value throughout the program

Example 

main()
{
int a=5;
cout<<"++a:"<<++a<<"\ta:"<<a<<endl;
cout<<"--a:"<<--a<<"\ta:"<<a<<endl;
cout<<"a++:"<<a++<<"\ta:"<<a<<endl;
cout<<"a--:"<<a--<<"\ta:"<<a<<endl;

}

O/P

++a:6 a:6
--a:5 a:5
a++:5 a:6
a--:6 a:5

Initially, ++a made a to 6 and –a will make 6 to 5, as a++ is post increment it is displayed with last updated value i.e 5 but a becomes 6 finally as a– is post-decrement 6 is displayed but it makes a to 5

Competitive Coding Techniques

Bitwise Operators

  • Bitwise operators apply true false conditions on the individual binary bits of numbers
  • They are used to manipulate the bits for digital processing and flipflops

There are 6 in total

  1. Bitwise AND(&): Returns true you only are both input bits are true otherwise false
  2. Bitwise OR(|): Returns true if either of the input bits is true otherwise false
  3. Bitwise XOR(^): Returns true only if both the input bits are different
  4. Bitwise 1’s COMPLIMENT(~): Converts true input Bit to false and false input Bit to true
    Shift-operators
  5. LeftShift(<<): Shifts the required number of bits to the Left
  6. RightShift(>>): Shifts the required number of bits to the right

Clearly, understand the difference between logical and bitwise operators

In the case of logical operators every input is considered as true other than  zero but here numbers are converted into binary format and operations are performed on individual bits and again the result is converted into the decimal format and then displayed

main()
{
cout<<(5&4);//4
cout<<(5|4);//5
cout<<(5^4);//1
cout<<(~5);//2
cout<<(5&&4);//1
//because true and true is true
cout<<(5||4);//1
//because true or true is true
cout<<(!5);//0
//not of true is false

}
Bitwise Operators in C++

Shift operators

Right shift

number>>no_of_bits_to_be_shifted
Formula: Number/2no_of _shifts

Left Shift
number<<no_of_bits_to_be_shifted
Formula: Number*2no_of _shifts

main()
{
cout<<(5<<2);//5*4=20
cout<<(5>>2);//5/4=1
}
Competitive Coding Techniques

Ternary Operator

The conditional operator is a decision-making operator whose statement is evaluated based on the test condition

Syntax:

(Test condition)? Statement 1: Statement 2

If the condition is true statement 1 is evaluated and if it is false, statement 2  is evaluated

Test whether a number is even or odd using the ternary operator

main()
{
int n;
cout<<"enter a num:";
cin>>n;
(n%2==0)?cout<<"\neven":cout<<"\nodd";//using ternary operator
}
  • Assume n equal to 4, 4%2==0 ->0==0 will result true you and even is printed
  • Assume n equal to 5 ,5%2==1-> 1==0 will result false and Odd is printed

Nesting Ternary Operator

Ternary operator can be nested i.e you can have a ternary operator inside another ternary operator

Find the maximum of three numbers using the ternary operator

main()
{
int a,b,c,res;
cout<<"Enter 3 numbers :";
cin>>a>>b>>c;
res=a>b?(a>c?a:c):(b>c?b:c);//nesting of ternary operator
cout<<"\nmaximum number is"<<res;
}

O/P

Enter 3 numbers: 1 2 3 
maximum number is 3

Comma Operator

The comma operator is a special operator which evaluate statements from left to right and returns rightmost expression as the  final result

C++ program to demonstrate comma operator

main()
{
int a=1,b=2,c;
c=(a=a+2,b=a+3,b=a+b);
//initially a=a+2 is evaluated which makes a as 3 then
// b=b+3 is evaluated which makes b as 6
// finally c=a+b is evaluated i.e 6+3 is the final result
cout<<c;//9
}