Difference between revisions of "Binary Nums"

From CompSciWiki
Jump to: navigation, search
(expanded explanation of modulus and integer division)
(new step: Test your Pseudocode,fixed misnamed variables in pseudocode)
Line 87: Line 87:
  
 
==Determine how to solve the problem==
 
==Determine how to solve the problem==
 +
If you already know how to convert from Binary Numbers to Decimal Numbers skip this section.
 
The greatest ways to start a problem is to divide it into smaller problems
 
The greatest ways to start a problem is to divide it into smaller problems
 
and understand which parts you need to focus on.
 
and understand which parts you need to focus on.
Line 191: Line 192:
 
  {
 
  {
 
     rightmost = binaryNum % 10  //right most digit  
 
     rightmost = binaryNum % 10  //right most digit  
     sum = sum + ''d'' * 2^exponent
+
     sum = sum + rightmost * 2^exponent
 
     binaryNum = binaryNum / 10; //do the same with the digits to the left of the rightmost digit
 
     binaryNum = binaryNum / 10; //do the same with the digits to the left of the rightmost digit
 
  }
 
  }
Line 218: Line 219:
 
</pre>
 
</pre>
  
To be continued...
+
==Test your pseudocode==
 +
Take the pseudocode for converting the numbers, and paste
 +
it into the main method. Then compile it and fix any errors. You will
 +
need to add semicolons, and declare variables.
 +
 
 +
You will need to add an output statement so you can test your results.
 +
As for input, just set the binaryNum variable in the code for now (this is called hardcoded input).
 +
 
 +
<pre>
 +
public class BinaryNum
 +
{
 +
  public static void main(String[] args)
 +
  {
 +
    int binaryNum = 101;
 +
    int exponent = 0;
 +
    int sum = 0;
 +
 +
    while (binaryNum!= 0)
 +
    {
 +
      rightmost = binaryNum % 10  //right most digit
 +
      sum = sum + rightmost * 2^exponent;
 +
      binaryNum = binaryNum / 10; //do the same with the digits to the left of the rightmost digit
 +
    }
 +
  }
 +
}
 +
</pre>
 +
 
 +
 
 +
 
  
  

Revision as of 12:33, 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.

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

Determine how to solve the problem

If you already know how to convert from Binary Numbers to Decimal Numbers skip this section. The greatest ways to start a problem is to divide it into smaller problems and understand which parts you need to focus on.

It is clear that the main problem to solve is converting binary numbers to decimal numbers. However, you will need to use the Scanner class, loops, and System.out . Let's start by trying to figure out how to convert from binary numbers to decimal numbers.

Convert from Binary Numbers to Decimal Numbers

So you don't know how to convert binary numbers to decimal numbers. That's great. We can figure it out.

To solve the problem, we will initially forget about writing good java code, and instead use anything simpler. We can draw diagrams, write equations, and write pseudocode. We can always write java code after we understand how to solve the problem.

The first thing to do is gather information from the problem specification. In it we were told:


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

If we look at this carefully, we see that the right most digit of the binary number is multiplied by 2^0. The second left most digit is multiplied by 2^1. The digit to the right of that is multiplied by 2^2 and so on. Finally, all those products are summed up.

However, we need to make sure we are correct. Let's check that we understand by converting some of the following numbers from binary to decimal.

  • 0
  • 1
  • 1
  • 10
  • 11
  • 101
  • 1000
  • 1101
  • 10101

Now, try to write some pseudocode to explain how we solved this problem. We will later shape this into Java code.

1. take the rightmost digit, 
       if it is a 1, multiply it by 2 to the power of the current exponent and add it to the sum.
2. do the same with digits to the left of the rightmost digit.

A little bit more thinking shows we need to tell what sum, and what exponent to start at. Anything multiplied by zero is zero, so we can simplify the pseudocode. Let's also add variables for clarity.

 exponent = 0
 sum = 0

 take the rightmost digit ''d'', 
       sum = sum + d * 2^exponent
 do the same with digits to the left of the rightmost digit.


Convert the pseudocode to Java-like pseudocode

How do I take the rightmost digit in Java

In Java, you have some basic arithmetic operators, you can multiply (using *), divide (using /), add (+), subtract (-), and get the remainder (%).

If you can define what the rightmost digit is using the above operations you can easily determine what it is.

The rightmost digit of a number is the exact same thing as the remainder of a number divided by 10. For example, the remainder of 26 divided by 10 is 6. In Java, we would write 26 % 10 to do the same operation.



How do I take the digits to the left of the rightmost digit in Java

So you want to convert 321 to 32.

Recall, that in Java the division of two integers ignores any fractions. So 3/2 =1, in Java code although it equals 1.5 in regular math. The way java calculates the quotient is called integer division.

To chop off the rightmost digit, we simply do integer divide by 10. In Java, we would write 26 /10 and the result would be 2.


You can continue improving the pseudocode slightly until you have replaced much of the English instructions with Java-like code. Here is an example of my result.

 exponent = 0
 sum = 0
 
 while (binaryNum!= 0)
 {
     rightmost = binaryNum % 10  //right most digit 
     sum = sum + rightmost * 2^exponent
     binaryNum = binaryNum / 10; //do the same with the digits to the left of the rightmost digit
 }


Now that you have some pseudocode, let's make sure that it is sound. Let's turn it into Java code and run it through the compiler.

Create the Structure of the Program

Now create a Java file.

Define the class in a Java file. Name the file the same name as the class name plus the ".java" extension. Define the main method.

The following code shows the result of these steps.

public class BinaryNum
{
  public static void main(String[] args)
  {
  }
}

Test your pseudocode

Take the pseudocode for converting the numbers, and paste it into the main method. Then compile it and fix any errors. You will need to add semicolons, and declare variables.

You will need to add an output statement so you can test your results. As for input, just set the binaryNum variable in the code for now (this is called hardcoded input).

public class BinaryNum
{
  public static void main(String[] args)
  {
    int binaryNum = 101;
    int exponent = 0;
    int sum = 0;
 
    while (binaryNum!= 0)
    {
      rightmost = binaryNum % 10  //right most digit 
      sum = sum + rightmost * 2^exponent;
      binaryNum = binaryNum / 10; //do the same with the digits to the left of the rightmost digit
    }
  }
}

Code

Solution Code

Back to the Case Studies homepage