Basics Of Operators||Basics of operators in C++||Basics of operators in cpp||Types of operators in C++||Arithmetic Operators||Comparision Operators||Binary Operators||Logical Operators||Bitwise Operators||Assignment Operators||Miscellaneous Operators||Bitwise Explained||Operators Precedence||


πŸ‘‰ Basics Of OPERATORS IN C++ πŸ‘ˆ


-by Aaditya Nayak


In this blog I am going to describe operators in C++ from scratch, their use ,applications, examples etc. This blog will be very useful for beginners and for experienced its always good to brush up your concepts.


What is an operator? 

β€’ An Operator gives the instruction perform particular operation.

Ex: A person which is operating a machine, keyboard of computer, mouse of computer etc.

 

β€’ Mathematically operators are symbols which denotes particular operation to be performed on a expression or on some numbers.

Ex: +, -, /, *, d/dx, βˆ‘, ∏ etc.

 

β€’ In programming an operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C++ language is rich in built-in operators and provides the following types of operators βˆ’ Arithmetic Operators. Relational Operators. Logical Operators.

Ex: &&, ||, &, | etc.


Operators in C++

Basic meaning of operators in C++ are same as in other programming language. In C++ operators are the instructions for compiler to perform specific Arithmetic, Relational, Logical, insertion, extraction etc. operation.

Types Of Operators in C++

1.On the basis of number of operands(elements on which operator gives instruction to operate on) there are two types of operators.


(i) Unary Operators

(ii) Binary Operators


(i) Unary Operators : 

                    The operators which acts on single operand are called unary operators.

                     Ex: ++, --, etc.


(ii) Binary Operators : 

                    The operators which acts on two operands are called binary operators.

                     Ex: +, -, &, |,  etc.


2.On the basis of operation for which the operation gives instruction for the are of following types: 

(i) Arithmetic Operators

(ii) Comparison Operators

(iii) Logical Operators

(iv) Bitwise Operators

(v) Assignment Operators

(vi) Miscellaneous Operators


(i) Arithmetic Operators : 

They give compiler instructions to perform arithmetic operations such as addition, subtraction, multiplication, etc.

Following are the list of arithmetic operators


Symbol

  Operator

Function

Example

+

Addition

Adds together two values

Ex : x + y = returns sum of x and y

-

Subtraction

Subtracts one value from another

Ex : x - y = returns difference of x and y

*

Multiplication

Multiplies two values

Ex : x * y => returns product of x and y

/

Division

Divides one value by another

Ex : x / y => returns quotient when x divided by y

%

Modulus

Returns the division remainder

Ex : x / y => returns remainder when x divided by y

++

Increment

Increases the value of a variable by 1

Ex : x++ => increases value of x by 1

--

Decrement

Decreases the value of a variable by 1

Ex : x-- => decreases value of x by 1


Lets understand it with a simple code : 


Code:


// All Arithmetic Operators in one Program

#include <iostream>
using namespace std;

int main()
{
    int x;
    int y;

    x = 20;
    y = 10;

    //(i) Addition (+)

    cout<<"(i) "<<x+y<<endl;

    //(ii) Subtraction (-)

    cout<<"(ii) "<<x-y<<endl;

    //(iii) Multiplication (*)

    cout<<"(iii) "<<x*y<<endl;

    //(iv) Division (/)

    cout<<"(iv) "<<x/y<<endl;

    //(v) Modulus (%)

    cout<<"(v) "<<x%y<<endl;

    //(vi) Increment (++)

        // (a) pre increment

            cout<<"(vi) (a) "<<++x<<endl; // Updated x value = 21

        // (b) post increment

            cout<<"(vi) (b) "<<x++<<endl; // Updated x value = 22

    //(vii) Decrement (++)

        // (a) pre increment

            cout<<"(vii) (a) "<<--x<<endl; // Updated x value = 21

        // (b) post decrement

            cout<<"(vii) (b) "<<x--<<endl; // Updated x value = 20

return 0;

}


Output:



Note : 

1. You can use increment and decrement operator as post or pre.


Ex: If x is initially 1 and you use:


       Case1: post increment 

                   cout<<x++;

Then it will show output as 1.


       Case2: pre increment 

                   cout<<++x;

Then it will show output as 2.


2. If you divide a number by 0 using / operator, it will cause runtime error.


3. By default all operators will return integer value.


(ii) Comparison Operators : 


β€’ They are used to compare two values or variables.


β€’ These operators returns Boolean value 1(true) if comparison is correct       otherwise returns 0(false) if comparison is incorrect.

Note : Here 1 and 0 are in binary form.


Ex: if x=5 and y=6 then x>y will return 0 and x<y will return 1.


β€’ Following are the list of comparison operators.


Operator Symbol

Function

Ex:

==

Equal to

x == y => returns 1 if x is equal to  y else return 0

!=

Not equal

x != y => returns 1 if x is not equal to y else returns 0

> 

Greater than

x > y => returns 1 if x is greater than y else returns 0

< 

Less than

x < y => returns 1 if x is less than y else returns 0

>=

Greater than or equal to

x >= y => returns 1 if x is greater than or equal to y else returns 0

<=

Less than or equal to

x <= y => returns 1 if x is less than or equal to y else returns 0


Lets understand it with a simple code : 


Code:

// All Comparision operators in one program.

#include <iostream>
using namespace std;

int main()
{
    int x;
    int y;

    x = 20;
    y = 10;

    //(i) equal to (==)

    cout<<"(i) "<<(x==y)<<endl;

    //(ii) Not equal (!=)

    cout<<"(ii) "<<(x!=y)<<endl;

    //(iii) Greater than (>)

    cout<<"(iii) "<<(x>y)<<endl;

    //(iv) Less Than (<)

    cout<<"(iv) "<<(x<y)<<endl;

    //(V) Greater than or equal to (>=)

    cout<<"(v) "<<(x>=y)<<endl;

    //(Vi) Less than or equal to (<=)

    cout<<"(vi) "<<(x<=y)<<endl;

return 0;

}


Output:







(iii) Logical Operators : 


Logical operators gives compiler instructions to perform logical operations such as and, or, not etc.


If you have studied logic gates then here also these operators works in same manner if you have not stay tuned I will explain it.


As we have discussed about Boolean values in Comparison operators we will return the value for logical operators by using that.


(i) Logical AND (&&)


If values of both operands are 1 then it returns 1 otherwise 0.

Or 

you can simply say that it returns product of Boolean values.


Truth Table:

x

y

returned value

0

0

0

0

1

0

1

0

0

1

1

1


(ii)  Logical OR (||)


If value of any one operand is 1 then it returns 1 otherwise 0.

Or 

you can simply say that it returns summation of Boolean values (In Boolean sum of 1 and 1 is 1).


Truth Table:

x

y

returned value

0

0

0

0

1

1

1

0

1

1

1

1

(iii) Logical NOT (!)


It is an unary operator.

If the value of operand is 1 then it returns 0 and vice – versa.

Or 

you can simply say that it returns complement of the operand value.


Truth Table:

x

returned value

0

1

1

0


Lets understand it with a simple code : 

Code:

// All Logical operrators in one program

#include <iostream>
using namespace std;

int main()
{
    int uvwxyz;

    u = 10v = 20;
    w = 10x = 20;
    y = 30z = 40;

    //(i) Logical AND (&&)

    cout<<"(i) "<<((x<y)&&(x>y))<<endl;

    //(ii) Logical OR (||)

    cout<<"(ii) "<<((x<y)||(x>y))<<endl;

    //(iii) Logical NOT (!)

    cout<<"(iii) "<<(!(x>y))<<endl;   

    return 0;
}


Output: 



 


(iv) Bitwise Operators : 


In this type of operator it will compare all the bits in binary form of the given operands and compare them by applying logical and, or, not or xor depends on which operator is used.


Following are the types of bitwise operators: 


(i) Bitwise AND (&)


When given two numbers as operand it will operate every bit of one number to the corresponding bit of another number using logical AND and the obtained binary number after operation of AND on each and every bit returned as output (in its decimal form).


Ex:

Binary representation of 10

0

0

0

0

1

0

1

0

&

&&

&&

&&

&&

&&

&&

&&

&&

Binary representation of 9

0

0

0

0

1

0

0

1

Binary representation of returned value Output which is 8

0

0

0

0

1

0

0

0

(ii) Bitwise OR (|)


When given two numbers as operand it will operate every bit of one number to the corresponding bit of another number using logical OR and the obtained binary number after operation of OR on each and every bit returned as output (in its decimal form).


Ex:

Binary representation of 10

0

0

0

0

1

0

1

0

|

||

||

||

||

||

||

||

||

Binary representation of 9

0

0

0

0

1

0

0

1

Binary representation of returned value Output which is 11

0

0

0

0

1

0

1

1


(iii) Bitwise XOR (^)


When given two numbers as operand it will operate every bit of one number to the corresponding bit of another number using logical XOR and the obtained binary number after operation of XOR on each and every bit returned as output (in its decimal form).


Lets first understand truth table of XOR

x

y

returned value

0

0

0

0

1

1

1

0

1

1

1

0

If value of both operands are different it returns 1 otherwise 0.


Ex:

Binary representation of 10

0

0

0

0

1

0

1

0

^

XOR

XOR

XOR

XOR

XOR

XOR

XOR

XOR

Binary representation of 9

0

0

0

0

1

0

0

1

Binary representation of returned value which is 3

0

0

0

0

0

0

1

1


(iv) Bitwise NOT (~)


Its an unary operator which gives compiler instruction to complement binary form of given operand and return the value of so obtained number in decimal form.

Or

In short you can remember by using operator ~ on a variable whose value is say : 14, it will return -14.


Ex:

Binary representation of 10

0

0

0

0

1

0

1

0

~

!

!

!

!

!

!

!

!

Binary representation of returned value which is 245 which is equivalent to

 -10

1

1

1

1

0

1

0

1


(v) Bitwise Shift (<< , >>)


(i) Bitwise right shift (>>)


This is a binary operator which shifts every bit to the right in a binary form of given operand to the number of places which are given as second operand.


The right shift operator (>>) shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Copies of the leftmost bit are shifted in from the left. Since the new leftmost bit has the same value as the previous leftmost bit, the sign bit (the leftmost bit) does not change. Hence the name "sign-propagating".


Ex: 

Binary representation of 10

0

0

0

0

1

0

1

0

>> 2

Right shift by 2 bits

Right shift by 2 bits

Right shift by 2 bits

Right shift by 2 bits

Right shift by 2 bits

Right shift by 2 bits

Right shift by 2 bits

Right shift by 2 bits

Binary representation of returned value which is 2

0

0

0

0

0

0

1

0


(ii) Bitwise left shift (<<)


This is a binary operator which shifts every bit to the left in a binary form of given operand to the number of places which are given as second operand.


Ex:

Binary representation of 10

0

0

0

0

1

0

1

0

<<2

Left shift by 2 bits

Left shift by 2 bits

Left shift by 2 bits

Left shift by 2 bits

Left shift by 2 bits

Left shift by 2 bits

Left shift by 2 bits

Left shift by 2 bits

Binary representation of returned value which is 40

0

0

1

0

1

0

0

0


Lets understand it with a simple code : 

Code:

// All Bitwise operrators in one program

#include <iostream>
using namespace std;

int main()
{
    int x = 10 ; int y = 9;

    //(i) Bitwise AND (&)

    cout<<"(i) "<<(x & y)<<endl;

    //(ii) Bitwise OR (|)

    cout<<"(ii) "<<(x | y)<<endl;

    //(iii) Bitwise XOR (^)

    cout<<"(iii) "<<(x ^ y)<<endl;  

    //(iv) Bitwise NOT (^)

    cout<<"(iv) "<<(~y)<<endl;  

    //(v) Bitwise Shift 

        //(a) Right Shift (>>) 

        cout<<"(v) (a) "<<(y>>2)<<endl;// Updated value of y is 2

        // Since y value changed by previous shift sioo reassigning y value
        
        y = 10;

        //(b) Left Shift (<<) 

        cout<<"(v) (b) "<<(y<<2)<<endl;

    return 0;
}


Output:








(vi) Assignment Operators : 

The assignment operators are used to assign values to a    variable.
Symbol of operator Example Same as (Function of operator)

Note: When you will learn more concepts in C++ you will also come across * as a dereferencing operator, . operator, -> operator etc.


Lets understand it with a simple code : 


Code:

// All assignment operators in one program

#include <iostream>
using namespace std;

int main()
{
    int x = 20 ; int y = 10int z;

    //(i) =

    cout<<"(i) From x = "<<x<<" the value of x is updated to ";

    z = x;

    cout<<z<<endl<<endl;

    //(ii) +=

    cout<<"(ii) From x = "<<x<<" the value of x is updated to ";

    x += y; // Same as : x = x + y

    cout<<x<<endl<<endl;

    //(iii) -=

    cout<<"(iii) From x = "<<x<<" the value of x is updated to ";

    x -= y; // Same as : x = x - y

    cout<<x<<endl<<endl;

    //(iv) *=

    cout<<"(iv) From x = "<<x<<" the value of x is updated to ";

    x *= y; // Same as : x = x * y

    cout<<x<<endl<<endl;

    //(v) /=

    cout<<"(v) From x = "<<x<<" the value of x is updated to ";

    x *= y; // Same as : x = x / y

    cout<<x<<endl<<endl;

    //(iv) %=

    cout<<"(vi) From x = "<<x<<" the value of x is updated to ";

    x %= y; // Same as : x = x % y

    cout<<x<<endl<<endl;

    //(vii) %=

    cout<<"(vii) From x = "<<x<<" the value of x is updated to ";

    x &= y; // Same as : x = x & y

    cout<<x<<endl<<endl;

    //(viii) %=

    cout<<"(viii) From x = "<<x<<" the value of x is updated to ";

    x |= y; // Same as : x = x | y

    cout<<x<<endl<<endl;

    //(ix) ^=
    
    cout<<"(ix) From x = "<<x<<" the value of x is updated to ";

    x ^= y; // Same as : x = x ^ y

    cout<<x<<endl<<endl;

    //(x) >>
    
    cout<<"(x) From x = "<<x<<" the value of x is updated to ";

    x >>= 2; // Same as : x = x>>2

    cout<<x<<endl<<endl;

    //(x) <<
    
    cout<<"(x) From x = "<<x<<" the value of x is updated to ";

    x <<= 2; // Same as : x = x<<2

    cout<<x<<endl<<endl;

    return 0;
}

    
Output:





 







Operator Precedence 

Operator precedence is used by the compiler when more than one operators are used in the single expression, the operators are executed in the order of their precedence. It is similar to BODMAS trick that you may have used in mathematics.


Ex: a + b – c * d


You can refer to the below table for operator precedence:


In the table given below follow the precedence order as given in table. In case the operators have same precedence follow associativity order as precedence.


Ex: If the expression is a * b / c (where * and / operator have same precedence)
      Then * will be executed first then / will be executed.
      If the expression is a / b * c (where * and / operator have same precedence)
      Then / will be executed first then * will be executed.


Note: In the table given below some operators may be that which are not described in this blog since in this blog since in this blog I have covered the basics of operators in C++, you will learn more of the operators when you will advance more in C++ and learning about them there will make more sense to you and you will understand them better there.


Category

Operator

Associativity

Postfix

(), [] ,->, ., ++, - -

Left to right

Multiplicative

* / %

Left to right

Additive

+ -

Left to right

Shift

<<, >>

Left to right

Relational

<, <=, >, >=

Left to right

Equality

==, !=

Left to right

Bitwise AND

&

Left to right

Bitwise XOR

^

Left to right

Bitwise OR

|

Left to right

Logical AND

&&

Left to right

Logical OR

||

Left to right

Conditional

?:

Right to left

Assignment

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

Right to left

Comma

,

Left to right



Now you are all done with the basic concepts of operators in C++, try using them and writing programs with them in your code editor or ide.



THANKS FOR READING THE BLOG 

I Hope This was helpful 😊




Comments

Popular posts from this blog

What Is Functions In C++ ? || Explained By Examples || Function Types

Reference and Pointers in C++