Bitwise Operator in C language
Bitwise operator in C language
Basicaly bitwise operator or those operators which, whenever be used perform
action on the bit values of the data.
- AND operator &
And operator perform AND operation on the bit values
of operands.
int a=2, b=3,c;
c=a+b;
since a = 2 which
have binary equivalent is 0010 and b=3 and can have binary equivalent is
0011.
So and operation will be performed like:
0 0
1 0
&
0 0 1
1
0 0
1 0
If both the bits
are 1 then output also will be 1 other wise 0.
2. OR operator |
OR operator perform OR operation on the Bit values of
Numbers.
int a=12,b=7,c;
c = a | b;
since a = 12 so its binary conversion is 1100 and b =
7 so binary conversion is 0111.
The OR operation will be performed like:
1 1
0 0
| 0 1
1 1
1 1 1 1
If any of the bit
is 1 then the output value is also 1.
3. XOR operator ^
This operator can perform XOR operation on numbers
assigned with it.
Let a , b and c
are three variable then XOR operation will be performed like
C = a ^ b;
If there is odd number of one in the bit sequence of
respective number then the output will also be 1 otherwise the output will be
0.
If a == b then
C = a ^ b = 0;
4. Bit wise operator
~ ( 1’s complement )
It will store one’s complement of the number. Since complement
is calculated using the bit sequence of
any number, so it is called bitwise operator.
5. Left shift Operator <<
It will shift the bit sequence by 1 position towards
left side.
A =
0
|
0
|
1
|
0
|
B =
a<<1;
B =
0
|
1
|
0
|
0
|
In the above case
b will store the value which is shifted bit stream by the left shift operator.
6.
Right shift operator >>
Right shift
operator will shift the bit stream towards right.
A =
0
|
0
|
1
|
0
|
B =
a>>1;
B=
0
|
0
|
0
|
1
|
New 0 will be
inserted at the starting bit and stream will shift toward right.
Some other facts and operators commonly used in C language:
·
Size
of operator will give amount of memory in bytes
Sizeof( a );
·
A
character constant is treated as integer and a float constant is treated as
double.
·
Comma
operator ( , ): the associatively of comma operator is left to right.
X=( a, b, c);
The
value of c will be assigned to x.
·
Size
of and ! have same and higher priority.
·
Comments
Post a Comment