Using the bitwise shift operators. - C++ Operator

C++ examples for Operator:Bit Operator

Description

Using the bitwise shift operators.

Demo Code

#include <iostream> 
#include <iomanip> 
using namespace std; 

void displayBits( unsigned ); // prototype 

int main() //from  ww w . j  a va2 s .c  o m
{ 
   unsigned number1 = 960; 

   // demonstrate bitwise left shift 
   cout << "The result of left shifting\n" ; 
   displayBits( number1 ); 
   cout << "8 bit positions using the left-shift operator is\n"; 
   displayBits( number1 << 8 ); 

   // demonstrate bitwise right shift 
   cout << "\nThe result of right shifting\n"; 
   displayBits( number1 ); 
   cout << "8 bit positions using the right-shift operator is\n"; 
   displayBits( number1 >> 8 ); 
}

// 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 << " = "; 

   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; 
}

Result


Related Tutorials