#include <string>
#include <iostream>
usingnamespace std;
int main()
{
ushort num;
string bits;
bits.reserve( sizeof( ushort ) * 8 ); // make sure it only allocates the needed memory
cout << "input: ";
cin >> num;
cin.ignore();
cout << "\nbinary value: ";
do
{
bits.insert( 0, num & 1? "1" : "0" ); // if unsure about this syntax, check "ternary operator"
num >>= 1; // this is a shift, it does the same as an optimised division by 2.
} // probably your compiler would generate this anyway
while( num );
cout << bits << std::endl;
return 0;
}
One strange thing is that input of -1 gives a value of 2052. This is trough some peculiarity of how cin interpretes "-1" to ushort... I don't have a clue where the value 2052 comes from though.