C++ examples for Operator:Bit Operator
Bitwise AND, inclusive OR, exclusive OR and complement operators.
#include <iostream> #include <iomanip> using namespace std; void displayBits( unsigned ); // prototype int main() /* www. j a v a 2 s .com*/ { unsigned number1; unsigned number2; unsigned mask; unsigned setBits; // demonstrate bitwise & number1 = 217999999; mask = 1; cout << "The result of combining the following\n"; displayBits( number1 ); displayBits( mask ); cout << "using the bitwise AND operator & is\n"; displayBits( number1 & mask ); // demonstrate bitwise | number1 = 15; setBits = 221; cout << "\nThe result of combining the following\n"; displayBits( number1 ); displayBits( setBits ); cout << "using the bitwise inclusive OR operator | is\n"; displayBits( number1 | setBits ); // demonstrate bitwise exclusive OR number1 = 119; number2 = 159; cout << "\nThe result of combining the following\n"; displayBits( number1 ); displayBits( number2 ); cout << "using the bitwise exclusive OR operator ^ is\n"; displayBits( number1 ^ number2 ); // demonstrate bitwise complement number1 = 21845; cout << "\nThe one's complement of\n"; displayBits( number1 ); cout << "is" << endl; displayBits( ~number1 ); } // display bits of an unsigned integer value void displayBits( unsigned value ) { const int SHIFT = 8 * sizeof( unsigned ) - 1; const unsigned MASK = 1 << SHIFT; cout << setw( 10 ) << value << " = "; // display bits for ( unsigned i = 1; i <= SHIFT + 1; ++i ) { cout << ( value & MASK ? '1' : '0' ); value <<= 1; // shift value left by 1 if ( i % 8 == 0 ) // output a space after 8 bits cout << ' '; } cout << endl; }