Thursday, April 2, 2015

Bitwise Operation in C and C++

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;


  1. & for Bitwise AND
  2. | for Bitwise OR
  3. ^ for Bitwise Exclusive XOR
  4. << for left shift
  5. >> for right shift
  6. ~ 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)

No comments:

Post a Comment