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
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
|
Code
Solution Code
import java.util.Scanner;
/**
* A2Q2 -
*
* COMP 010 Section A00
* INSTRUCTOR
* ASSIGNMENT 2, Question 2
* @author 1010
* @version 2010-Oct-18
*
* PURPOSE: calculates the decimal version of a binary number.
*/
public class A2Q2{
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
long binaryNumber, copyOfBinNum; // input from the user.
int decimalNumber; // output value.
final int BASE = 2; // binary base.
int currDigit, currPower; // values used in loops.
// get input from the user.
System.out.print("Enter a binary number:");
binaryNumber = kbd.nextLong();
// keep looping until -1 is encountered.
while (binaryNumber != -1) {
// reset values to 0, use copyOfBinNum for calculations.
decimalNumber = 0;
currPower = 0;
copyOfBinNum = binaryNumber;
// keep looping until all digits of copyOfBinNum are removed.
while (copyOfBinNum > 0) {
// get last digit and strip it off copyOfBinNum
currDigit = (int) copyOfBinNum % 10;
copyOfBinNum /= 10;
// add to the running total if the digit is one
if (currDigit == 1) {
decimalNumber += Math.pow(BASE,currPower);
}
// increment the power of two for next loop.
currPower++;
}
// output the right decimal value.
System.out.println(binaryNumber + " in decimal is " + decimalNumber + ".");
// get ready for next iteration of the loop.
System.out.print("Enter a binary number:");
binaryNumber=kbd.nextLong();
}
System.out.println("Programmed by [your name here].");
System.out.println("End of processing.");
}
}
|
Back to the Case Studies homepage