computer science lab#2

fadoo
LabCH021.pdf

© Dmitriy Shironosov/ShutterStock, Inc.

LABORATORY Exploring Number Systems

2

■■ Experiment■with■number■systems.■ ■■ Gain■practice■adding■in■binary■and■converting■between■bases.

OBJECTIVES

REFERENCES

■Software■needed: ■ 1)■ Number■Systems■app■from■the■Lab■Manual■website■(Number■Systems.jar) ■ 2)■ Binary■Addition■app■from■the■Lab■Manual■website■(Binary■Addition.jar)

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

Exploring Number Systems | 7

BACKGROUND Review these topics from your textbook, lecture notes, or online resources:

■ Number systems and positional notation ■ Binary, octal, and hexadecimal numbers ■ Converting between bases ■ Binary addition

ACTIVITY ❯ Part 1

First, review positional notation of numbers in several bases, including decimal (base 10), binary (base 2), octal (base 8), and hexadecimal (base 16). See your textbook, lecture notes, or online resources.

Bring up the Number Systems app and experiment with converting a few numbers, both from decimal to the other base and from the other base to decimal. For example, try the following exercises:

■ Type the decimal number 26 into the top text area, and press the Convert button next to it (or simply press Return after you type the decimal number). You will see “11010”, which is 26 expressed in binary.

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

8 | Laboratory 2

■ Now try converting in the other direction. Type “7777” into the Base X text area. This time, before you press the Convert button, you need to tell the app what base 7777 is a numeral of. Could it be binary? Base 5? Even base 7? (If you’re not sure, review positional notation.)

7777 could be decimal, octal, or hexadecimal. Pull down the choice bar and select Octal. Now you can press Convert!

What did you get? 4095? Try verifying that the app didn’t make a mistake (always a smart thing to do, considering that apps are software, and software is written by people, and people make mistakes).

7 3 80 5 7 3 1 5 7 7 3 81 5 7 3 8 5 56 7 3 82 5 7 3 64 5 448 7 3 83 5 7 3 512 5 3584 4095

Some calculator programs, such as the Windows Calculator, also allow you to convert between decimal, binary, octal, and hexadecimal.

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

Exploring Number Systems | 9

❯ Part 2 Now you will hone a skill crucial to a budding computer scientist—binary addition. While it might seem unfamiliar at first, binary addition works much the same as addition in the decimal system you know and love, where adding numbers in a column will sometimes generate a carry value that must be added to the column to the left.

First, review binary addition from your textbook, lecture notes, or online resources. Make sure you understand the process of generating a carry from one column to the next one to the left. Here’s a handy review table:

0 0 0 0 0 1 1 1

10 11 10 11 0 1 1 0

The smaller number floating to the left and above the bigger numbers is, of course, the carry. It must be added to the next column to the left, unless it is in the last column, in which case it just drops down.

One thing that textbooks seldom show is a table where the carry-in is added to the two digits, producing the sum bit and the carry-out. The reason is probably due to space constraints, since two digits and a carry-in would give eight combinations. However, we can quickly cut that down to four combinations, because four of the possible combinations are the same as previously shown, when the carry-in is 0. So here’s the useful part of the three-bit addition table:

Using this table, see if you can do the binary addition of 101110 and 11011 yourself. Remember that if there is no bit, just pretend there is a 0.

Now start the Binary Addition app. Type “5” into the top box on the far right, labeled Decimal. Type “7” into the box below it. We want to start off with easy numbers so we can verify the app ourselves.

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

10 | Laboratory 2

Now press the Add button and watch it go. First, the app converts 5 to binary, which is 101, and stashes that in the top binary box. Then it converts 7 to binary (111) and tucks that into the bottom box. Next, it splits those binary numbers into bits and pads them on the left side with 0s. (Why do you suppose computers insist on putting “useless” 0s on the left side of numbers?) Finally, the app animates its addition, column by column.

When it is done, you will see 00001100 both in the Binary box and split up into bits. Can you convert that into decimal? You can confirm on the app screen that it’s 12.

Many of the apps in this lab manual have an Example button (or buttons) that you should explore. The Example button usually displays a harder task than you might think of inputting yourself. In later labs, the examples show you complex patterns that would take too long to type in. In any case, press it now to see what the app does.

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

Exploring Number Systems | 13

DEEPER INVESTIGATION Many anthropologists believe that most human societies use base 10 (decimal) for their counting system because we have 10 fingers. (Why people don’t use their toes as well, hence favoring a base 20 system, is a good question. Perhaps during the Ice Age, people had to keep coverings on their feet when they tramped from continent to continent!)

There are a few interesting discrepancies. For example, the ancient Babylonians had a base 60 system, which shows itself still today in our minutes and seconds, and in our 360-degree compasses.

Imagine what kinds of problems you would have to solve if you insisted on using a base 60 system. What would your calculator keypad look like? Would it be practical?

An even deeper question is what base 1 would look like. Base 2, 3, 4, 5, . . . 8, . . . 10, . . . 16, even 60 are all very similar. In base X, there are X symbols, from 0 up to (X − 1). These symbols are multiplied by powers of X. But what about base 1? If you multiply 1 by any power, you still get 1. However, base 1, also called unary, actually has a place in theoretical computer science. Describe what a base 1 number would look like and how you could convert between decimal and unary.

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION