SI204 Fall 2002 Lab #2: Types

Part 1, Reading Binary Numbers

Background

As you recall, numbers can be represented in various bases. A base describes how many unique digits are used is representing the number. For example, we mostly represent numbers in base 10; there are 10 different digits used to represent the numbers: 0,1,2,3,4,5,6,7,8,9. Numbers can be represented in many (infinite!) other bases, such as base 8. In base 8 the numbers are represented with only 8 digits: 0,1,2,3,4,5,6,7.

The actual value of a number depends on both what digits are used, and the location of the digits. Obviously, the number 123 is smaller than the number 321. In particular the farther left a digit is, the more contribution it makes to the size of the number. If we number the columns from right to left, starting with 0, then when the base is b and the digit d is in column c, the the contribution of that digit to the value of the number is d*bc. For example the number 123 base 10 (written 12310) is 3*100 + 2*101 + 1*102, which totals 123. For a less obvious example, 1238 is 3*80 + 2*81 + 1*82, which totals 83.

The reason you're reading all this is because computers don't store their numbers in base 10, but rather in base 2 (the only digits are 0 and 1). As computer programmers, we need to be able to convert numbers from the binary (base 2) format to the decimal (base 10) format. That is what you will do for the lab.

What to do

You will write a program that converts a number from binary to decimal format. You will then print out the decimal value of that number.

  1. Prompt the user to enter a 4-bit binary number (Recall that binary digits are also called bits). You may assume that the number the user enters really is a binary number.
  2. Read the number in as four separate characters.
  3. Convert the four bits to a single decimal number.
  4. Print out the decimal value of the number.

Part 2, Converting Back to Binary

Background

Just as one might want to convert from binary to decimal, one might want to perform the conversion the other direction (decimal to binary). The basic principle is the same, except instead of multiplying by powers of two, we need to divide (and mod) by the powers of two. For example, if the decimal number is 11 and we want to convert to a 4 bit binary number, then the leftmost bit (column 3) is 11/(23), or 1.

What to do

You will write a program that converts a number from decimal to binary format. You will then print out the binary value of that number. You need to figure out how to perform the conversion. You know the first step.

  1. Prompt the user to enter a decimal number between 0 and 15, inclusive. You may assume that the user really does enter a number in that range.
  2. Read in the number as an integer.
  3. Convert the number into four binary digits.
  4. Print out the four digits.

Challenge Part, A Different Way of Converting Decimal to Binary

Background

There is a different way of converting from a decimal number to a binary one, but starting with the rightmost bit, rather than the left. It involves division by the base (not raised to any power) repeatedly.

What to do

Determine how to generate a binary number from a decimal number, starting with the rightmost bit. Repeat part 2 using this technique.