DO NOT MISS

Ads

Tuesday 21 June 2016

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

Greataims

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