Download this source file


// Demo of manipulator that show the bit pattern for a number.
// Written 1999 by Wayne Pollock, Tampa Florida USA.

#include <climits>
#include <iostream>

using namespace std;

class bits
{
  unsigned long num;

  public:
   bits ( unsigned long n ) : num( n ) {}
   friend ostream& operator<< ( ostream& out, const bits& b );
};

ostream& operator<< ( ostream& out, const bits& b )
{
   unsigned long bit = ~( ULONG_MAX >> 1 );  // 10000...
   while ( bit )
   {
      out << ( (bit & b.num) ? '1' : '0' );  // note bitwise-and.
      bit >>= 1;  // shift bit one place to the right.
   }
   return out;
}


int main ()
{
   int num = 145;
   cout << "The bit pattern for " << num << " is: " << bits(num)
        << endl;
   return 0;
}

#ifdef COMMENTED_OUT
		Output of above program:

The bit pattern for 145 is: 00000000000000000000000010010001
#endif