A: 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;
if ( i % 8 == 0 ) // output a space after bits cout << ' ';
}
cout << endl;
}
You may do the same by divide by 2, till the number is greator than 0. However you ought to use stack to print it in reverse order.