Bitwise Shift Operators

Figure 20.11 demonstrates the left-shift operator (<<) and the right-shift operator (>>). Function displayBits (lines 27–45) prints the unsigned integer values.

 1   // Fig. 20.11: fig20_11.cpp 2   // Using the bitwise shift operators. 3   #include <iostream> 4   #include <iomanip> 5   using namespace std; 6  7   void displayBits( unsigned ); // prototype 8  9   int main()10   {11      unsigned number1 = 960;12 13      // demonstrate bitwise left shift14      cout << "The result of left shifting\n";15      displayBits( number1 );16      cout << "8 bit positions using the left-shift operator is\n";17      displayBits( number1 << 8 );18 19      // demonstrate bitwise right shift20      cout ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.