Bitwise Operator In C++ Language
Bitwise
operators are used for manipulating a data at the bit level, also called as bit
level programming. Bit-level programming mainly consists of 0 and 1.
There are six type of bitwise operator in c language.
1)
Left Shift Bitwise Operator (<<)
2)
Right Shift Bitwise Operator (>>)
3)
Inverse Bitwise Operator (~)
4)
Bitwise AND Operator (&)
5)
Bitwise OR Operator (|)
6)
Exclusive Bitwise Operator (^)
Fig: Bitwise Operator |
1.Left Shift Bitwise Operator (<<)
The left shift operation will shift the 'n' number of bits to
the left side.
Example:
a=13;
a<<2;
2.Right Shift Bitwise Operator(>>)
The right shift operation will shift the 'n' number of bits
to the right side.
Example:
a=13;
a>>8;
3.Inverse Bitwise Operator(`)
The bitwise inverse is also called as one's complement
operator since it always takes only one value or an operand. It is a unary
operator.
Example:
a=7;
Binary digit=111
`a=000
4. Bitwise AND Operator (&)
Bitwise
AND operators, It is represented by a
single ampersand sign (&). Two integer expressions are written on each side
of the (&) operator.
The
result of the bitwise AND operation is 1 if both the bits have the value as 1;
otherwise, the result is always 0.
Example:
a b a&b
0 0 0
0 1 0
1 0 0
1 1 1
a=10101
b=01010
a&b=0000
5.Bitwise OR Operator (|)
It is represented by a single vertical bar sign (|). Two
integer expressions are written on each side of the (|) operator.
Example:
a=0000 1101
a1=0001 1001
6. Bitwise Exclusion Operator (^)
It
is represented by a symbol (^). Two integer expressions are written on each
side of the (^) operator.
Example:
a=0000 1101
a1=0001 1001
Program
#include<iostream.h>
#include<conio.h>
void
main()
{
int a=60;
int b=13;
int
c=0;
c=a&b;
cout<<“Line1-value
is c”;
c=a|b;
cout<<“Line2-value
is c”;
c=a^b;
cout<<Line3-value
is c”;
c=`a;
cout<<“Line4-value
is c”;
c=a>>2;
cout<<“Line5-value
is c”;
c=a<<2;
cout<<“Line6-value
is c”;
getch();
}
Output:
Line1-
value is c 12
Line2-
value is c 61
Line3-
value is c 49
Line4-
value is c 61
Line5-
value is c 240
Line6-
value is c 15
No comments:
Post a Comment