Printing a Binary Representation of an Integral Value

When using the bitwise operators, it’s useful to illustrate their precise effects by printing values in their binary representation. The program of Fig. 20.6 prints an unsigned integer in its binary representation in groups of eight bits each.

 1   // Fig. 20.6: fig20_06.cpp 2   // Printing an unsigned integer in bits. 3   #include <iostream> 4   #include <iomanip> 5   using namespace std; 6  7   void displayBits( unsigned ); // prototype 8  9   int main()10   {11      unsigned inputValue = 0; // integral value to print in binary12 13      cout << "Enter an unsigned integer: ";14      cin >> inputValue;15      displayBits( inputValue );16   } // end main17

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.