The bitwise
operators perform bit by bit -AND (&), exclusive-OR (^), OR (|), RIGHT-SHIFT
(>>) and LEFT-SHIFT (<<) operations.
Example :
#include
<iostream.h>
//Example of Operator ^ & | << >>
class
Bit_Op
{
private:
int
n;
public:
Bit_Op()
{
n=0;
}
void
input();
void disp();
void Deci_Bin();
Bit_Op operator &(Bit_Op);
Bit_Op operator ^(Bit_Op);
Bit_Op operator |(Bit_Op);
Bit_Op operator <<(Bit_Op);
Bit_Op operator >>(Bit_Op);
};
void Bit_Op::Deci_Bin()
{
int n1,i=0,j,a[15];
n1=n;
while(n1>0)
{
a[i++]=n1%2;
n1=n1/2;
}
for(j=i-1;j>=0;j--)
{
cout<<a[j];
}
}
void
Bit_Op::input()
{
cout<<"\n\tEnter no ";
cin>>n;
}
void Bit_Op::disp()
{
cout<<" "<<n;
}
Bit_Op Bit_Op::operator ^(Bit_Op O)
{
Bit_Op OP;
OP.n=n^O.n;
return OP;
}
Bit_Op Bit_Op::operator &(Bit_Op O)
{
Bit_Op OP;
OP.n=n&O.n;
return OP;
}
Bit_Op Bit_Op::operator |(Bit_Op O)
{
Bit_Op OP;
OP.n=n|O.n;
return OP;
}
Bit_Op Bit_Op::operator <<(Bit_Op
O)
{
Bit_Op OP;
OP.n=n<<O.n;
return OP;
}
Bit_Op Bit_Op::operator >>(Bit_Op
O)
{
Bit_Op OP;
OP.n=n>>O.n;
return OP;
}
int main()
{
Bit_Op O1,O2,O3,O4;
O1.input();
O2.input();
cout<<"\n\tNos.
";
O1.disp();
cout<<" &
";
O2.disp();
cout<<" Result ^ ";
O3=O1^O2;
O3.disp();
cout<<"\n\tBinary ";
O1.Deci_Bin();
cout<<" &
";
O2.Deci_Bin();
cout<<" Result ^ ";
O3.Deci_Bin();
cout<<"\n\n\tNos.
";
O1.disp();
cout<<"
& ";
O2.disp();
cout<<" Result & ";
O3=O1&O2;
O3.disp();
cout<<"\n\tBinary ";
O1.Deci_Bin();
cout<<" &
";
O2.Deci_Bin();
cout<<" Result & ";
O3.Deci_Bin();
cout<<"\n\n\tNos.
";
O1.disp();
cout<<"
| ";
O2.disp();
cout<<" Result | ";
O3=O1|O2;
O3.disp();
cout<<"\n\tBinary ";
O1.Deci_Bin();
cout<<" | ";
O2.Deci_Bin();
cout<<" Result & ";
O3.Deci_Bin();
cout<<"\n\n\tNos.
";
O1.disp();
cout<<" <<
";
O2.disp();
cout<<" Result << ";
O3=O1<<O2;
O3.disp();
cout<<"\n\tBinary ";
O1.Deci_Bin();
cout<<" <<
";
O2.Deci_Bin();
cout<<" Result << ";
O3.Deci_Bin();
cout<<"\n\n\tNos.
";
O1.disp();
cout<<"
>> ";
O2.disp();
cout<<" Result >> ";
O3=O1>>O2;
O3.disp();
cout<<"\n\tBinary ";
O1.Deci_Bin();
cout<<"
>> ";
O2.Deci_Bin();
cout<<" Result >> ";
O3.Deci_Bin();
return
0;
}
No comments:
Post a Comment