Lecture 22 - Binary


As usual, create two directories for today's class. Create a directory called lecture22 under activities, and a directory called lab22 under labs.


Binary

Everything in a computer can be represented as a number. We saw this fact in practice using the ord function in the previous lecture, to convert characters to their appropriate integer value. However, this is much more simple than what is actually going on. Today, you will learn how to encode numbers using binary.


In-Class Activity 1

Parsing Integers

Create a function parse_int that takes as input a string, and returns the decimal integer representation of the number in the string. Your function should assume that the input string is completely numeric (all of the characters are numeric characters). DO NOT USE THE BUILT IN int FUNCTION.

Don't forget to define your test cases. Your program should execute your test cases automatically.

Challenge Your code above likely assumed, rightfully so, all values input were positive. However, negative base-10 integers are still valid integers. Re-work your parse_int function so that it can handle a leading minus sign (-), and return the correct numeric value for the input.

In-Class Activity 2

Parsing Binary

In the same file as before, write a function called parse_binary, which takes as input a string, and also returns a base-10 integer representation of that string. However, in this case your string will only contain the characters 0 and 1. DO NOT USE THE BUILT IN int FUNCTION.

Don't forget to define your test cases. Your program should execute your test cases automatically.

Challenge It is very typical in binary to not use a minus sign (-) to define negative numbers. Instead, the most significant bit is typically used to define the sign of the number. If the most significant bit is 0 the number is positive, and if it is 1 the number is negative. change your parse_binary function to accomodate for this fact.

Lab Assignment 22

Base Conversion

The same structure demonstrated in class for binary can be used in general for an arbitrary base. Although we've only discussed decimal (base 10) and binary (base 2), any arbitrary number can be used as a base for a number system. In this assignment, you will write code to convert numbers into an arbitrary base.

Create a file called base_conversion.py in your lab22 directory. You are to write a function called convert_base that takes two parameters: an integer of the number to convert, and the base to convert that integer to (some number in the range [2, 10]). This function should return a string representation of the input parameter in the specified base.

Make sure you test your program well. How many test cases do you need? Include your test cases in your program file. Comment them appropriately.

Challenge

Another very popular numeric system is known as hexadecimal. hexadecimal is simply a base 16 number system. However, we only have 10 "numeric" digits to work with in our number system. We typically get around this by using letters for the rest of the numbers. So, 'A' is used to represent the digit 10, 'B' for the digit 11, and up to 'F' representing the digit 15.

Alter your convert_base function to allow for conversions of bases up to 16. In an ideal world, this change would actually account for bases up to 36. Recall that you can use the chr function to convert a number into an ASCII character.

Challenge

Everyone knows that the answer to Life, the Universe, and Everything is 42. We just don't know what the question is. ...Well actually, we do know the question. The question is:

What do you get when you multiply 6 by 9?

While it seems that this is absurdly incorrect, the calculation is correct if you assume the correct base. Write a function find_base that returns the base of the number system where 6 × 9 actually equals 42


Submission

When you have finished, create a tar file of your lab22 directory. To create a tar file, execute the following commands:

cd ~/cs120/labs
tar czvf lab22.tgz lab22/

To submit your activity, go to cseval.roanoke.edu. You should see an available assignment called Lab Assignment 22. Only one of your pair should submit your activity. Make sure both partners are listed in the header of your files.

Do not forget to email your partner today's files!


Last modified: Mon Oct 21 17:05:35 EDT 2013