To manipulation of binary (bit) value, we generally perform various operation. Some of them are supported by C and C++ programming language. There are basically six BITWISE operation available for C and C++ are;
- & for Bitwise AND
- | for Bitwise OR
- ^ for Bitwise Exclusive XOR
- << for left shift
- >> for right shift
- ~ for bitwise NOT
Here is the C++ code example for Bitwise operation
#include<iostream>
using namespace std;
class Bitwise
{
int a,b,r;
public:
Bitwise()
{ }
void GetNumber()
{
cout<<"\t\t ENTER TWO DECIMAL NUMBERS"<<endl;
cin>>a>>b;
}
void BitAnd()
{
r=a & b;
cout<<"\t\t BITWISE AND RESULT"<<endl<<"\t\t"<<r<<endl;
}
void BitOR()
{
r=a | b;
cout<<"\t\t BITWISE OR RESULT"<<endl<<"\t\t"<<r<<endl;
}
void BitXOR()
{
r=a ^ b;
cout<<"\t\t BITWISE XOR RESULT"<<endl<<"\t\t"<<r<<endl;
}
};
int main()
{
Bitwise b1;
b1.GetNumber();
b1.BitAnd();
b1.BitOR();
b1.BitXOR();
return 0;
}
Output is (ALL INPUT AND RESULT ARE IN DECIMAL)
#include<iostream>
using namespace std;
class Bitwise
{
int a,b,r;
public:
Bitwise()
{ }
void GetNumber()
{
cout<<"\t\t ENTER TWO DECIMAL NUMBERS"<<endl;
cin>>a>>b;
}
void BitAnd()
{
r=a & b;
cout<<"\t\t BITWISE AND RESULT"<<endl<<"\t\t"<<r<<endl;
}
void BitOR()
{
r=a | b;
cout<<"\t\t BITWISE OR RESULT"<<endl<<"\t\t"<<r<<endl;
}
void BitXOR()
{
r=a ^ b;
cout<<"\t\t BITWISE XOR RESULT"<<endl<<"\t\t"<<r<<endl;
}
};
int main()
{
Bitwise b1;
b1.GetNumber();
b1.BitAnd();
b1.BitOR();
b1.BitXOR();
return 0;
}
Output is (ALL INPUT AND RESULT ARE IN DECIMAL)
No comments:
Post a Comment