Now let's start with the operators in c++
An operator operates on one or more operands to produce results
There are two types of operators based on the number of operands
1> unary operators : operates on a single operand
eg : -2
2> binary operators : operates on two operands
eg : 2+3
In c++ operators are classified as :
1> arithmetic operators : These operators are used to apply mathematical operations on operands.
eg: 2+3, 2*3, 2/3, 2%3
a%b
where the '%'(modulus) operator is the operator which produces the remainder when a is divided by b
Assignment operators(=) :
a=b;
This operator is used to store the value of b to a.
cascading '=' operators
a=b=c=2;
storing 2 in c then the value of c in b and then the value of b in a.
Shorthand in c++ :
int a += 1;
the above expression is known as short hand in c++
this is equivalent to a = a + 1;
similarly a *= 2;
this is equivalent to a = a*2;
Relational operators :
these operators are used to combine logical expressions
eg:
a == b
this expression is used to check the equivalence of the value of a and b
that is it returns true when the value of a and b are equal
in c++ true is equivalent to 1
a != b
this expression is used to check the non-equivalence of the value of a and bthat is it returns true when the value of a and b are not equal
e.g. 3 != 2 => true
a > b
this expression is used to test whether the value of a is greater than b or notthat is it returns true when the value of a is greater than b
e.g. 3>2 => true
a < b
this expression is used to test whether the value of a is less than b or not
that is it returns true when the value of a is less than b
e.g. 2<3 => true
and
a<=b and b>=a
Logical and bitwise Operators
1> and
a> &&(logical)
exp1 && exp2 this checks both the expression exp1 and exp2 and returns true when both of them are true.
the following is the truth table A => exp1 and B => exp2
b> &(bitwise)
val1 & val2 converts val1 and val2 into binary and then applies and operation to each bit specifically
e.g. 2 & 3
2 => 010
3 => 011
2&3 => 010(2)
(look at the table above to understand the result)
2> or
2 => 010
3 => 011
2&3 => 010(2)
(look at the table above to understand the result)
2> or
a> ||(logical)
exp1 || exp2 this checks the expression exp1 and if found to be true returns true if not then checks the expression exp2 and returns true if it is true.
the following is the truth table A => exp1 and B => exp2
b> |(bitwise)
val1 | val2 converts val1 and val2 into binary and then applies or operation to each bit specifically
e.g. 2 & 3
2 => 010
3 => 011
2&3 => 011 (3)
(look at the table above to understand the result)
2 => 010
3 => 011
2&3 => 011 (3)
(look at the table above to understand the result)
4> NOT(logical) :
!(expr) inverts the ouptput of the expression
3> XOR (bitwise)
val1 ^ val2 al1 | val2 converts val1 and val2 into binary and then applies or operation to each bit specifically
the following is the truth table A => exp1 and B => exp2
0 => true and 1 => false
e.g. 3^2
2 => 011
3 => 010
2^3 => 001 (1)
(look at the table above to understand the result)
(look at the table above to understand the result)
4> right shift(bitwise operator) :
a>>i this expression converts the value of a into binary then shifts the position of the last(right most) one to the right by i positions(ok!!! yeah!!!! what????)
e.g 32>>2
100000 >> 2 shifting the position of the 1(right most one) by 2 positions to the right
001000(8)
which is equivalent to 32 / (2^2) (nice isn't it????)
a>>i this expression converts the value of a into binary then shifts the position of the last(right most) one to the right by i positions(ok!!! yeah!!!! what????)
e.g 32>>2
100000 >> 2 shifting the position of the 1(right most one) by 2 positions to the right
001000(8)
which is equivalent to 32 / (2^2) (nice isn't it????)
5> left shift(bitwise operator) :
a<<i this expression converts the value of a into binary then shifts the position of the last(right most) one to the left by i positions
e.g 32<<2
100000 << 2 shifting the position of the 1(right most one) by 2 positions to the right
10000000(128)
which is equivalent to 32 * (2^2)
Ternary operator(conditional operator) :
(expr) ? st1 : st2;
if expr is true than st1 is executed and if false than st2 is executed
e.g 32<<2
100000 << 2 shifting the position of the 1(right most one) by 2 positions to the right
10000000(128)
which is equivalent to 32 * (2^2)
Ternary operator(conditional operator) :
(expr) ? st1 : st2;
if expr is true than st1 is executed and if false than st2 is executed
Precdence of operators : It is the property of a operator that decides if multiple operators are available which one is executed in what sequence
that is it decides the sequence of execution
following table is from www.cplusplus.com
Type casting : this is a process of converting one data type to another
general syntax :
Destination data type variable = (destination type) Source data type variable
Destination data type variable = (destination type) Source data type variable
there are two types of type casting
a> implicit typecasting : size of destination data type > size of source data type.
int a = 2;
float fa;
fa = a;
cout<<fa;
b> explicit typecasting : size of destination data type < size of source data type
It is depicted in the picture above.
when there are more than one data type in an expression than all the variables in the expression are converted to the data type with the largest size among them.(if sizes match or if floating data types exist then floating data type is given precedence)
e.g. int a=12, b=13; float c=1;
cout<<(a*c)/b; //here and b are converted to the float data type
here is the snippet of the program showing all kinds of operators in c++
programs name is operators.cpp
is snippet of the ouput
The increment(++) and decrement(--) operator
syntax :
int a=2;
++a;//increments the value of the variable by 1 that is after the execution of
//this statement the value of a is 3
Two types of increment operators :
1> pre increment : It's main mantra is first increment then use.
e.g. int a=2;
cout<<++a;//first increases the value of a to 3 then
//prints a
output : 3
2> post increment : It's main mantra is first us then increment.
e.g. int a=2;
cout<<a++;//first prints the value of a i.e. 2 then
//increases a to 3
output : 2
the source file of the programs are uploaded in google drive in Learning c++ folder
No comments:
Post a Comment