Difference between revisions of "Binary Nums"

From CompSciWiki
Jump to: navigation, search
m (corrected formula in problem definition (added exponent symbols))
m (grammatical correction of first program specification)
Line 60: Line 60:
 
=Program Specifications=
 
=Program Specifications=
 
<ul>
 
<ul>
<li>Write a program that takes input as an integer, interprets it as a binary number, and prints out its value of the decimal number.
+
<li>Write a program that takes input as an integer, interprets it as a binary number, and prints out its value as a decimal number.
 
</li><li> Get input using Scanner.
 
</li><li> Get input using Scanner.
 
</li><li> Use integer division and remainder to extract digits from the input binary number.
 
</li><li> Use integer division and remainder to extract digits from the input binary number.

Revision as of 10:19, 27 March 2011

Back to the Case Studies homepage

Problem

As you may know, computers work on the binary system. In this system, numbers are written using only 0s and 1s. In this question, you’ll write a program to convert binary numbers to normal (decimal) numbers.

How are all numbers represented in 0s and 1s?

  • Instead of having digits 0,1,...,9, in binary, we only have digits 0 and 1.
  • So zero is 0, and the next number is represented by 1, but after that, we have no digit 2, so we have to represent the next number in the same way we do after we go past 9 in the decimal system: by 10.
  • We continue in this way: 3 in binary is 11.
  • Now what about 4? We can’t write 4 as 12 (because we only have 1 and 0 to work with, not 2). So we put a zero in the last position, and carry the 1. This would give 20, but again, we don’t have a digit 2, so we again carry the 1 and get 100 as the binary representation of 4.

Here are the first few numbers written in both decimal (normal) notation and binary:


decimal 1 2 3 4 5 6 7 8 9 10 11
binary 1 10 11 100 101 110 111 1000 1001 1010 1011

Suppose we have a binary number (a sequence of zeroes and ones) and we want to convert it to a decimal number. To do this, we multiply the digit (0 or 1) in each position of the number by an increasing power of two the further we move left. So the value of the binary number 1011001 is

1 * 2^6 + 0 * 2^5 + 1 * 2^4 + 1 * 2^3 + 0 * 2^2 + 0 * 2^1 + 1 * 2^0 = 89


Program Specifications

  • Write a program that takes input as an integer, interprets it as a binary number, and prints out its value as a decimal number.
  • Get input using Scanner.
  • Use integer division and remainder to extract digits from the input binary number.
  • Assume the user only inputs numbers with digits 0 and 1.
  • Use a loop so the user can continue to input binary numbers until they input -1 , then exit.
  • Here is a sample run of the program:
    Enter a binary number:1001010
    1001010 in decimal is 74.
    Enter a binary number:1001111
    1001111 in decimal is 79.
    Enter a binary number:10000111
    10000111 in decimal is 135.
    Enter a binary number:-1
    Programmed by [your name here].
    End of processing.
    

Output

Use System.out for output. Follow the format of the output shown above.

Hand-in

Save your file using the naming conventions described in the beginning of the assignment. Upload your file using the web-based hand-in tool. Submit three sets of data using the following input:

  1. 1000011
  2. 1011
  3. A binary number of your choice

You may notice that your program fails for large numbers. That's because of integer overflow. If you would like to get your program to work for larger binary numbers, replace int with long in your program (including using the Scanner method nextLong()).

 

Solution

Code

Solution Code

Back to the Case Studies homepage