Exercise #9


Suppose we wanted to write a C++ program that repeatedly prompts for and reads a number into a variable of type int.  After reading in a number, the program must display the int value as a 32-bit binary number; i.e., in 1's and 0's.  The program should keep doing this until the number 0 is entered.  You may assume that each input value is a positive number.

Here are two examples:

For the number 9802,
the program displays    00000000000000000010011001001010

For the number 1862108953,
the program displays   01101110111111011000011100011001
 
 
 
 
 
 

--------------------------------------------------------------------------------------------------
 
 
 
 

Suppose we wanted to extend the program by allowing the input of negative numbers, as well as positive ones.  What could be done to be sure that negative numbers such as the following ones are correctly displayed in binary form?

For the number -144166,
the program displays  11111111111111011100110011011010

For the number -713556423,
the program displays  11010101011101111111111000111001