Printing an unsigned integer in bits
#include <iostream>
#include <iomanip>
using namespace std;
void displayBits( unsigned );
int main()
{
unsigned x;
cout << "Enter an unsigned integer: ";
cin >> x;
displayBits( x );
return 0;
}
void displayBits( unsigned value )
{
unsigned c, displayMask = 1 << 15;
cout << setw( 7 ) << value << " = ";
for ( c = 1; c <= 16; c++ ) {
cout << ( value & displayMask ? '1' : '0' );
value <<= 1;
if ( c % 8 == 0 )
cout << ' ';
}
}
Related examples in the same category