‘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.
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
|
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
|
If x=10, calculate x<<3.
Bit-wise Right Shift:-
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
|
If x=10, calculate x>>3.
Bit-wise complement:-
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
|
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
|
Post a Comment