DO NOT MISS

Ads

Showing posts with label Types of Operators in C-Lang. Show all posts
Showing posts with label Types of Operators in C-Lang. Show all posts

Tuesday, 21 June 2016

Unary, Conditional & Special Operators

Conditional operator:-
A ternary operator " ? : " is variable in 'C' is to construct conditional expressions of the form
exp 1 ? exp 2 : exp 3;


Unary Operator:-
These operators on a single value or variable. 
These are __, ++, --, !.

Special operators:-
“,”(Comma) & Size of are the special operators.
“,” is used to separate the variables size of operator is used to find the size of the data types.
These are 
&(address operator), * (indirection operator)
., size of.

Bit-Wise operators


‘C’ has distinction of supporting special operators known as bit-wise operators for manipulation of date at bit level.
These operators are used for testing the bits or shifting them right or left.
Bit-wise operator may not be applying to float or double.

Operator
Meaning
&
Bit-wise AND
|
Bit-wise OR
^
Bt-wise Exclusive (or) XOR Operator
<< 
Shift left
>> 
Shift right
~
Bit-wise NOT (or) Complement



Bit-wise AND :-
Two variables x & y whose values are 10 & 15 respectively.
Calculate Z=X & Y.

Bit-wise AND
X=10          -  0000  0000  0000  1010
Y=15          -  0000  0000  0000  1111
Z=X&Y          -  0000  0000  0000  1010  =  10

Bit-wise OR:-
Calculate Z=X|Y

Bit-wise OR
X=10          -  0000  0000  0000  1010
Y=15          -  0000  0000  0000  1111
Z=X|Y         -  0000  0000  0000  1111  =  15

Bit-wise XOR:-
Calculate Z=X^Y

Bit-wise Ex-OR (XOR)
X=10          -  0000  0000  0000  1010
Y=15          -  0000  0000  0000  1111
Z=X^Y          -  0000  0000  0000  0101  =  5


Bit-wise Left Shift:-
If x=10, calculate x<<3.

Bit-wise Left Shift
X=10           -  0000  0000  0000  1010
Z=X<<3       -  0000  0000  0101  0000  =2*2*2*2 + 2*2*2*2*2*2 
                                                 = 16 +64=80

Bit-wise Right Shift:-
If x=10, calculate x>>3.
Bit-wise  Right Shift
X=10            -  0000  0000  0000  1010
Z=X>>3            -  0000  0000  0000  0001  =1
Y=15            -  0000  0000  0000  1111
Z=Y>>3            -  0000  0000  0000  0001  =1

Bit-wise complement:-
Unary operator & inverts all the bits represented by its variables. That is 0’s becomes 1’s & 1’s becomes 0’s.
Bit-wise Complement
X=10            -  0000  0000  0000  1010
Z=X<<3            -  0000  0000  0000  0101  =5

Increment & Decrement operators


'C' has two very useful operators not generally found in other languages. They are increment and decrement operators ++ and --.
The operator ‘++’ adds 1 to the variable, while ‘--‘subtracts 1. Both the operators are unary operators & the fallowing form.

Increment & Decrement operators
a++ (post increment)
a-- (post decrement)
++a (pre increment)
--a (pre decrement)


++a or a++
++a --> a=a+1
--a or a--
--a --> a=a-1

While ++a & a-- mean something when form statements independently.

Example:-
Let a=5, b=a++
(post increment)
Let a = 5, b = ++a
(pre increment)
b=5, a=a+1 --> 5+1=6
b=++a --> 5+1=6
b=5, a=6
a=6, b=6


They behave differently when they are used in expressions from the right hand side of assignment statements. 
In the 1st case, a post fix operators 1st assigns the value to the variable on left & the increments the operator. In the 2nd case a prefix operator 1st adds 1 to the operator & then the result is assigned to the variable on left.


Increments operators
a=10, b=a++
b=10
a=11
a=10, b=++a
b=11
a=11
a=10, b=a++ + a++
b=20
a=12
a=10, b=a++ + ++a
b=22
a=12
a=10, b=++a + a++
b=22
a=12
a=10, b=++a + ++a
b=24
a=12
a=10, b=a++ + a++ + a++
b=33
a=13
a=10, b=a++ + a++ + ++a
b=34
a=13
a=10, b=a++ + ++a + ++a
b=35
a=13
a=10, b=++a + a++ + a++
b=34
a=13
a=10, b=++a + a++ + ++a
b=35
a=13
a=10, b=++a + ++a + a++
b=35
a=13
a=10, b=++a + ++a + ++a
b=36
a=13

Decrements operators
a=10, b=a--
b=10
a=9
a=10, b=--a
b=9
a=9
a=10, b=a-- + a--
b=19
a=8
a=10, b=a-- + --a
b=18
a=8
a=10, b=--a + a--
b=18
a=8
a=10, b=--a + --a
b=17
a=8
a=10, b=a-- + a-- + a--
b=27
a=7
a=10, b=a-- + a-- + --a
b=27
a=7
a=10, b=a-- + --a + a--
b=27
a=8
a=10, b=a-- + --a + --a
b=25
a=7
a=10, b=--a + a-- + a--
b=27
a=7
a=10, b=--a + a-- + --a
b=24
a=7
a=10, b=--a + --a + a--
b=25
a=7
a=10, b=--a + --a + --a
b=24
a=7
a=10, b=a++ - a++ - a++
b=-10
a=-13
a=10, b=a++ - ++a – a++
b=-11
a=-13
a=10, b=a++ - --a + a++
b=10
a=11

Greataims

 
Copyright © 2016 Computersadda. Designed by @ Computersadda Team - Published By Greataims