CS 130 Lab #2: Operations on Binary Numerals
This lab is short, like the last one, but is more focused on technical issues
and will probably be more challenging, at least for some students.
Due date: please have your answers to the questions below available
by the end of lab on Monday 6 February.
Generating "your" numbers
In this lab we will be doing various conversions and transformations on numbers;
in order to encourage students to do their own work (the practice that the lab provides
is only worthwhile if you do it yourself!), we will need some unique numbers that
"belong" to you, numbers that no one else in the class is likely to have.
We will therefore use your name and birthdate to generate
some numbers that are uniquely yours: let's call them your
"small number" and your "large number":
- to generate your small number, take the number of the month you were
born in, drop any first digit, and append the day you were born on on
that month, including a leading zero if the day number is less than 10.
For example, if you were born on November 5th, your number is 105, where
the leading "1" has been dropped from month number 11, and the leading
zero inserted since 5 is less than 10. (March 31st would yield 331.)
- to generate your large number, take the initials of your first and last
name and figure out which letters of the alphabet they are (my initials
are "FR", so the numbers are 6 and 18). If the first number is less than
10 (one digit), put a 1 in front of it. If the second number is less than
10, put a 3 in front of it. Now put the two numbers together, side by side,
to get your "large number" (so I would get 1618).
Some resources and tools for binary numeral calculation
These are just repeats from the course homepage, for your convenience here in the lab write-up:
Addition of binary numerals
Convert your large and small numbers into binary notation using the techniques
from lecture. (You may check your work using a calculator or on-line tool,
but I recommend doing the work itself by hand, since you won't have access to
these tools on quizzes and exams.)
(Yes, yes, the large number in particular will no doubt involve an annoyingly large
number of binary digits. But one of the hidden purposes behind this lab is to
motivate ways of "abbreviating" large binary numbers so that they can be written
and remembered more efficiently, but also easily converted back to their original
binary form.)
Now add the two numbers together, in binary,
being careful to show all your work in the tabular format usual for addition
(i.e., numbers right-aligned, digits in columns, "carries" shown in the usual
way, etc.). Be prepared to show this work during your demo, and perhaps to
do some more binary addition "live".
If you have trouble adding the two numbers together, look again at the
reference material above. Remember that it's
just like adding numbers in decimal, working place by place from right to left,
except there are fewer cases to handle:
- 0 and 0: write down a 0;
- 0 and 1 (or 1 and 0): write down a 1;
- 1 and 1: write down a 0, but carry the 1 (1 and 1 make 2, which in binary is "10");
- if you have to add in a carry, it either reduces to one of the previous
cases or (horrors!) you have 1 carry plus 1 and 1: this makes 3, which
in binary is "11", so ...: write down a 1 and carry a 1.
Multiplication of binary numerals
How do we multiply numbers in decimal? There is a popular method (or algorithm)
called "long multiplication" which you probably learned in grade school: it uses
a fixed layout for the numbers, a few lines and some simple rules.
We lay out two numbers, one above the other,
aligning their digits so that they are flush to the right. We then proceed
through the digits of the lower number from right to left. For each digit,
we multiply this digit by the upper number, and write it down below the line,
remembering to move the result one place to the left each time. Now we add the
columns of digits together, writing down the rightmost digit of the result
and carrying any other digits into the next column:
Multiplying binary numbers is almost the same, except easier:
- when we multiply by a 1 digit, we can just copy the other number
into the result.
- when we multiply be a 0 digit, we can just write a zero line in the result
(just like in decimal, but it comes up more often in binary!).
- of course, we still move successive results to the left and then add
the columns; this last part is only a little harder, since you may not be used
to adding binary in your head. But just count the number of 1 digits,
convert this (small) number to binary, then write down the rightmost
bit and carry the rest (you can even write the carries in decimal, if you like;
I used binary carries in the picture just for fun).
Special cases
Multiply your large number by eight, using decimal notation, by hand. Note the
steps that you use, the carries, etc., that make the task at least a bit
bothersome. Now take the converted binary version of your large number and
multiply it by eight, using the binary version of the technique. What do you
notice when you compare the original number and the result, i.e., how are
they related (in the simplest terms)?
What you need for your demo
- your large and small numbers, in decimal and binary forms;
- the addition of these two numbers in binary, written out in full,
in columns, with carries, etc.
- the ability to add two smallish numbers in binary "on demand";
- the multiplication of your two numbers in binary, written out in full;
- your large number (in decimal) multiplied by eight (in decimal),
written out in full;
- knowledge (based on experience) of how a number in binary can be
multiplied by eight very quickly.