Tutorial On Operators
Operators are the signs to the tell the compiler to perform specific task. The Operators fall into following categories :-
Arithmatic Operator
Relational Operator
Logical Operator
Bitwise Operator
Miscellaneous Operator(i added Assignment Operators in this categeory)
Arithmatic operators:-
There are following arithmetic operators:-
+ Add [6+2=8]
- Subtract [6-2=4]
* Multiply [6*2=12]
/ Divide [6/2=3]
% Modulus(Reminder) [7%2=1]
Relational Operator :-
Relational Operators are used to compare 2 values and result in true [1] or false [0].
< less than
> greater than
<= less than equal to
>= greater than equal to
== equal to
!= Not equal to
Lets take an example:-A=5 and B=10
CONDITION RESULT
A<B TRUE
A>B FALSE
A<=B TRUE
A>=B FALSE
A==B FALSE
A!=B TRUE
Logical Operators:-
Logical operator are used to combine more than one expression .
&& = LOGICAL AND
|| = LOGICAL OR
! = LOGICAL NOT
&& and || are binary operator whereas ! is unary operator. && and || are called binary because both work on 2 operand where ! works on single operand
&& Operator Produce 1[true] if the all expressions are true. And it produce 0[false] if any one expression is false. let’s see it truth table .
EXP1 EXP2 RESULT
TRUE TRUE TRUE
TRUE FALSE FALSE
FALSE TRUE FALSE
FALSE FALSE FALSE
Lets see the example below(a=10,b=20,c=30
)
Expression Expression evaluation Result
a<b && a<c true && true True
a<b && a>c true && false False
|| Operator produce 1[true] if either any one expression or all the expressions are true. And it produce 0[false] if both expression are false. Let’s see its truth table.
EXP1 EXP2 RESULT
TRUE TRUE TRUE
TRUE FALSE TRUE
FALSE TRUE TRUE
FALSE FALSE FALSE
Lets see the example below(a=10,b=20,c=30
)
Expression Expression evaluation Result
a<b || a<c true || true true
a<b || a>c true || false true
a==b || a>c false || false false
! Operator:- if the result is true then it convert it to false and if the result is false then it convert it to true. Any number except 0 is true and 0 itself for false. Lets see its truth table
Exp Result
TRUE FALSE
FALSE TRUE
Lets see the example below(a=10,b=20
)
Expression Expression Evaluation Result
!(a<b) !(tue) false
Bitwise Operators
& Bitwise And
| Bitwise OR
^ Bitwise Exlusive OR(XOR)
~ Bitwise one’s complement
<< Bitwise Left Shift
>> Bitwise Right Shift.
The & operator performs bitwise AND on 2 integers . Each bit in the result is 1 when both corresponding bits are 1.
Lets see the example below(a=23 , b=11
)
a = 23 or 0001 0111
b= 11 or 0000 1011
a&b = 0000 0011 [which is equivalent to 3]
The | operator performs bitwise OR on 2 integers . Each bit in the result is 1 when any one bit or both bits are 1 in the inputs.
Lets see the example below(a=23 , b=11
)
a = 23 or 0001 0111
b= 11 or 0000 1011
a|b = 0001 1111 [which is equivalent to 31]
The ^ Operator performs bitwise XOR which produce 1 in each bit if only one , but not both of the corresponding bits in the input is 1.
Lets see the example below(a=23 , b=11
)
a =23 or 0001 0111
b =11 or 0000 1011
a^b = 0001 1100 [which is equivalent to 28]
The ~ Operator performs on single operand (that’s why it is a unary operator ) And it return bitwise one’s complement of its oerand. That is, every bit that is 1 in the operand is 0 in the result. Conversely, every bit that is 0 in the operand is 1 in the result. The operand to the one's complement operator must be an integral type.
Lets see the example:-
0000 0000 0000 1011 (16 bits representation of integer 11)
So, ~11 is 1111 1111 1111 0100
The << Operator shifts its first operand left by a number of bits given by its second operand, filling in new 0 bits at the right.
Lets see the example:-
2<<3 [left operand is 2(0000 0000 0000 0010) and right operand is 3]
The result will be = 0000 0000 0001 0000 [which is equivalent to 16].
The >> Operator shift the first operand right by a number of bits given by its second operand.
16>>2 [left operand is 16(0000 0000 0001 0000) and right operand is 2]
The result will be = 0000 0000 0000 0100.[which is equivalent to 4].
Miscellaneous Operator:-
There are also operators that don’t fall into any of the above category . That’s why i put these all remaining operators in category named “Miscellaneous Operators” . And these are:-
Just consider a=10 and b=2
Operator purpose Example
= for the purpose of assignment a=b (initialise value of b to a)
+= a+=b is similar to a=a+b a+=b results in 12
-= a-=b is similar to a=a-b a-=b results in 8
*= a*=b is similar to a=a*b a*=b results in 20
/= a/=b is similar to a=a/b a/=b results in 5
Sizeof Return the size in bytes of the operand.sizeof(int)
++ increase the value by 1 ++a result in 11
-- decrease the value by 1 --b results in 1
Note : ++ and -- are unary operator , its mean that these operators work with one operand . and they can be pre and post .
Pre post
++a a++
--b b--
sometimes people think that both are same but in fact both are different. lets see Example:-
int a,b;
a=10;
b=a++; b=++a;
b=a++
will first initialise b
to the value of a
then a
gets incremented.b=++a
will first increment the value of a
and then initialise it to b
.b=a++
Here b
will be 10 and a
will be 11b=++a
Here b
will be 11 and a
will also be 11
Its all about what i’ve got in the chapter titled operator . I see some other operators also but books and some other resources say that these will be discussed later. So whenever i read these remaining operator , i will definitely add these operators to this tutorial. As i beginner and this tutorial is completely made with my own efforts and there may be some mistake , if anybody findout any mistake in the tutorial then it will a good opportunity for me to learn something new.
Here i would like to give special thanks to the person here with user name deceptikon who helped me a lot for making concept clear And he also encourage me to upload my own tutorial here.