Lab#3
© Dmitriy Shironosov/ShutterStock, Inc.
LABORATORY Representing Numbers
3
■■ Learn■how■negative■numbers■and■real■numbers■are■encoded■inside■computers.
■Software■needed: ■ 1)■ Apps■from■the■Lab■Manual■website:■ a)■ Negative■Binary■Numbers■(Negative■Binary■Numbers.jar) b)■ Real■Number■Representations■(Real■Number■Representation.jar)
OBJECTIVE
REFERENCES
© 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
Representing Numbers | 15
BACKGROUND Review these topics from your textbook, lecture notes, or online resources:
■ Representing negative values, signed-magnitude form, two’s-complement notation ■ Representing real numbers
ACTIVITY ❯ Part 1
There are a number of ways to encode negative numbers in binary. Two common methods are signed-magnitude representation and two’s-complement representation.
To begin, start the Negative Binary numbers app. In the top text area, type a negative number, such as “−67.” Either press Return or click the Convert button, and you should see the following:
Every representation needs to know the number of digits in the binary number, and that is set in the second text area. Eight is a common number of digits (we should be proper and call these binary digits “bits”), because a byte contains eight bits and the byte is widely used today as the smallest unit of addressable memory. (The analogy between bits and bytes and eating was too irresistible for early computer scientists, who noticed that a four-bit unit was also quite useful, especially in early coding schemes such as binary-coded decimal [BCD]. What do you suppose they called the four-bit unit? A “nibble,” of course, since it is half of a byte.)
The sign bit is highlighted in gray in the two bottom text areas. Try typing in a positive number and look at the forms. What sign bit do you see?
© 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
16 | Laboratory 3
❯ Part 2 Real numbers are, well, real important to computer applications. Early computer scientists had a hard enough time figuring out the best way to represent integers. But the need for real numbers arose from the hard sciences—physics, chemistry, engineering. Computer manufacturers invented a number of ways to encode the vital pieces of a real number for science and even business. Some times these representation methods were incompatible with other computers. Control Data Corporation (CDC) was one of these maverick manufacturers.
Eventually, an international standard, IEEE 754, was agreed upon, and it has been used in all chips since. (IEEE is the Institute of Electrical and Electronics Engineers, and 754 is the number of the document defining the standard.) You can rest assured that your computer’s central processor chip uses IEEE 754 encoding.
To begin, start the Real Number representations app and type in some numbers with fractional parts, such as “2.5” and “–373.4698.” Press Return or click the Format button. This app reformats the decimal real number three ways: in scientific notation, as a decimal fraction multiplied by a power of 2, and as a binary fraction.
Let’s study the number 2.5. First, 2.5 is already between 0 and 10, so we merely multiply it by 100 (which is 1) in order to get it into scientific notation. Next, we note that the app said 2.5 = 0.625 × 22. Since 22 is 4, 4 × 0.625 is 2.5. The 0.625 part of the number is referred to as the “mantissa,” which is the part of the number between the sign bit and the exponent. Most real number representations (including IEEE 754) place the decimal point in the leftmost position of the mantissa and make sure there is a 0 to the left of it. This contrasts with scientific notation, which specifies that the digit left of the decimal point must be larger than 0 but smaller than 10.
© 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
Representing Numbers | 17
Finally, let’s look at the binary representation at the bottom of the screen. 0.101 is not a decimal number but a binary one. Positional representation can be extended so that digits (or bits) to the right of the decimal point are multiplied by appropriate powers of 2. The first place after the 1’s position (which is 20) is the halves position, or 2–1. Since 2–1 is 0.5, we call this spot “the halves place.” The second place to the right of the point is the fourths place, or 2–2, which is 0.25. But since there is a 0 in that place in 0.101, we do not add 0.25 to our ongoing sum. Finally, the eighths place has a 1 in it, so we add 0.125, which is 1 ÷ 8, or one-eighth.
Adding 0.5 and 0.125, we get 0.625, which is our original mantissa. Multiply that by 22, which is 4, and we get 2.5, our original number.
Notice that a 1 appears in the two smaller text areas to the left of the numbers if the original number is negative. Real numbers use signed-magnitude representation for the overall number.
© 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
22 | Laboratory 3
DELIVERABLES Turn in your handwritten sheets showing your answers to the exercises.
DEEPER INVESTIGATION Computers love binary because of the bistable devices out of which we build them. These devices have only two stable states, ideal for representing 0 and 1 but nothing else. But the fact that we like to see numbers in decimal form and computers work in binary leads to some problems, similar to the impossibility of representing 1/3 exactly as a decimal fraction.
In the Real Numbers Representations app, type in “0.333” and press Return. Write down what you see in the scientific notation area. Then type in “0.3333” and repeat. Are you surprised? Why do you suppose the app prints out this weird value?
Now type in “0.1” (one-tenth) and press Return, looking at the binary representation at the bottom of the screen. It is a bit deceptive to look at what the app puts out. Try to come up with some combination of the negative powers of 2 that exactly equals 0.1. Can you do it? What does that mean about a computer representing 0.1? (You mean a computer can’t even represent 10 cents?!?!?!)
Because of these inaccuracies in number representations, called “roundoff errors,” early computers that were destined for business used a different method of representing dollars and cents, a method that would not be subject to roundoff errors. It was called the “packed decimal” method. See if you can find out more about the packed decimal online.
Failing that, figure out how one could use integers to represent dollar amounts exactly. What kinds of problems would still arise? (Hint: Think about interest rates!)
© 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