Download packet3 - UML Computer Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

History of logarithms wikipedia , lookup

Location arithmetic wikipedia , lookup

Addition wikipedia , lookup

Arithmetic wikipedia , lookup

Elementary mathematics wikipedia , lookup

Approximations of π wikipedia , lookup

Positional notation wikipedia , lookup

Transcript
Goals
To create a representation of integers which
1) Allows negative numbers to be represented.
2) Has only one representation of zero.
3) Allows numbers to be added as if they were unsigned binary
numbers.
-2
5
Add them as
if they were
unsigned
Both operands and the result should use the same representation.
3
Signed Magnitude
Representation
S
Magnitude
Basic Idea
If S is 0 the number is positive.
If S is 1 the number is negative.
Problem 1: Two representations of zero.
Problem 2: Cannot add them as if they were unsigned and always get the
right answer.
Ones Complement
Representation
Basic Idea
Positives are just like signed magnitude.
Flip the Bits to get the negative.
The ones become zeroes and the zeroes become ones.
Problem 1: Two representations of zero.
Problem 2: You can add them as if they were unsigned, but you need to
add back in the end carry. This wastes time.
Twos Complement
Basic Idea
Positives are just like signed magnitude.
Flip the bits and then add one to get the negative.
1 followed by all zeroes is the most negative integer.
Computers often use a twos complement representation for integers
because:
1) We want to represent negative numbers too.
2) There is one representation of zero.
3) We can build cost effective and time efficient circuits to
perform unsigned binary addition.
4) We can add two twos complement numbers as if they were
unsigned, ignore the end carry, and it always produces the correct
number. This is not true for either a signed magnitude
representation or a ones complement representation.
IEEE 754 Single Precision
Floating Point Format
You are given 32 bits.
The leftmost bit is the sign bit.
The next 8 bits represent the exponent.
The exponent uses an excess 127 notation.
The final 23 bits represent the mantissa.
There are normalized and denormalized numbers.
Normalized numbers use an implied one.
There are special cases.
What do you need to know
how to do?
Take a decimal fraction and be able to convert it into its IEEE 754
equivalent.
Basic Idea
Step 1: Form the binary fraction equivalent.
Step 2: Move the binary point to the right of the leftmost one.
Keep track of how many positions you moved it. Did you move it left
or right?
Step 3. Adjust 127 by the number of positions you moved the
binary point. Add if moved it left. Subtract if moved it right.
Step 4: Record the sign bit.
Step 5: Record the 8 exponent bits.
Step 6: Take all the bits to the right of the binary point and
record them. The implied one is not recorded, but it is understood
to be there.
Let’s do some on the board.
ASCII
What do you need to know?
1) What does ASCII stand for?
American Standard Code for Information Interchange
2) How to read an ASCII chart. Your text book has an its ASCII
chart on page 725.
3) Four specifics pieces of information. They are:
a) That “all zeroes” is called the null byte.
b) That the space, also known as blank, is decimal 32.
c) That upper case letters and their lower case counterparts
are off by 32.
d) That the character for number zero is decimal 48. From
this you should be able to recall all the digits.
4) In C an ASCII character is represented as an unsigned integer
using the decimal number that it has been assigned . For example,
the letter ‘A’ is decimal 65. This is 00100001.
Write out the answer as a sequence of 0s and
1s.
The character 3
using the ASCII
chart
The integer 3 using a
32 bit twos
complement
representation
The number 3.0 using
an IEEE 754
representation.
Unit 2: Signed Magnitude Representation Study Guide
91.101 Computing I
You need to know …
U2-1) how to convert a decimal number into its n-bit signed magnitude equivalent.
U2-2) how to convert an n-bit signed magnitude number into its decimal equivalent.
U2-3) what is the largest and smallest n bit number that can be represented as a signed magnitude integer.
U2-4) that adding two signed magnitude numbers as if they were unsigned does not necessarily produce a
correct answer.
U2-5) that a signed magnitude representation has two representations of zero.
Question 1:
Fill in the table below.
Decimal
Value
2 Bit Signed Magnitude
+1
0
0
-1
Question 2: If you are given two bit positions what is the largest decimal number that could be represented
using a signed magnitude representation?
Question 3: If you are given two bit positions what is the smallest decimal number that could be represented
using a signed magnitude representation?
Question 4:
Fill in the table below.
Decimal Value
7
4 Bit Signed Magnitude
6
5
4
3
2
1
0
0
-1
-2
-3
-4
-5
-6
-7
Question 5: If you are given 4 bits, what is the largest decimal number that could be represented using a signed
magnitude representation?
Question 6: If you are given 4 bits, what is the smallest decimal number that could be represented using a
signed magnitude representation?
Question 7: Fill in the table below.
Number of Largest decimal number that could
Bits Given be represented in signed magnitude.
1
Smallest decimal number that could
be represented in signed magnitude.
2
3
4
5
6
7
8
9
10
11
12
Question 8: If you were given n bit positions what is the range of decimal values that can be represented using
a signed magnitude scheme? Your response should use n in an expression.
Question 9: Please represent the decimal number 5 as a four bit signed magnitude number.
Question 10: Please represent the decimal number -2 as a four bit signed magnitude number.
Question 11: Please add your answer to question 9 to your answer to question 10. Perform the addition as if
you were adding two unsigned numbers.
Question 12: Look at the result you obtained in the previous problem. Interpret these bits as if they were a
signed magnitude representation. What is the decimal equivalent of these bits if they are to be interpreted as a
signed magnitude number? Notice that your answer is not 3.
Question 13: Please list two flaws with a signed magnitude representation.
Question 14: Suppose you have 12 bits to work with. Please represent the following decimal numbers using a
signed magnitude representation.
Decimal Value
500
12 Bit Signed Magnitude
-500
18
-18
Question 15: The following 16 bit pattern is a signed magnitude representation of some decimal number. What
decimal number is being represented?
0 0 0 0 0 1 1 1 1 0 0 0 1 1 1 11
Question 16: Fill in the table below.
Here is the correct
spelling of a word.
Please print the correct spelling of the work here.
receive
separate
programmer
integer
bookkeeper
Question 17: I have noticed that students often confuse the word then with the word than. Many times I see the
word then being misused when a student is comparing two quantities. For example, there is a tendency to write
something like “I would rather be at college feeling the freedom, joy, and power of learning then sitting on the couch.”
This is a classic misuse of the word then. The correct word is than.
Try not to do this.
Unit 3: Ones Complement Representation
91.101 Computing I
You need to know …
U3-1) how to convert a decimal number into its n-bit ones complement equivalent.
U3-2) how to convert an n-bit ones complement number into its decimal equivalent.
U3-3) what is the largest and smallest decimal number that can be represented as an n-bit ones complement
number.
U3-4) that adding two ones complement numbers as if they were unsigned will always produced a correct result
provided you add the leftmost carry-out back into the sum. This, of course, will cause a time delay.
U3-5) that the ones complement representation has two representations of zero.
Question 1:
Fill in the table below.
Decimal
Value
2 Bit Ones Complement
+1
0
0
-1
Question 2: If you are given two bit positions what is the largest decimal number that could be represented
using a ones complement representation?
Question 3: If you are given two bit positions what is the smallest decimal number that could be represented
using a ones complement representation?
Question 4:
Fill in the table below.
Decimal Value
7
4 Bit Ones Complement
6
5
4
3
2
1
0
0
-1
-2
-3
-4
-5
-6
-7
Question 5: If you are given 4 bits, what is the largest decimal number that could be represented using a ones
complement representation?
Question 6: If you are given 4 bits, what is the smallest decimal number that could be represented using a ones
complement representation?
Question 7: Fill in the table below.
Number of Largest decimal number that could
Bits Given be represented in ones complement.
1
Smallest decimal number that could
be represented in ones complement.
2
3
4
5
6
7
8
9
10
11
12
Question 8: If you were given n bit positions what is the range of decimal values that can be represented using
a ones complement scheme? Your response should use n in an expression.
Question 9: Please represent the decimal number 5 as a four bit ones complement number.
Question 10: Please represent the decimal number -2 as a four bit ones complement number.
Question 11: Please add your answer to question 9 to your answer to question 10. Perform the addition as if
you were adding two unsigned numbers. If there is a leftmost carry out, add this bit back into the sum.
Question 12: Look at the result you obtained in the previous problem. Interpret these bits as if they were a ones
complement representation. What is the decimal equivalent of these bits if they are to be interpreted as a ones
complement number? Notice that your answer is 3. If you did not get 3, then you need to redo the problem.
Question 13: Please list two flaws with a ones complement representation.
Question 14: Suppose you have 12 bits to work with. Please represent the following decimal numbers using a
ones complement representation.
Decimal Value
500
12 Bit Ones Complement Number
-500
18
-18
Question 15: The following 16 bit pattern is a ones complement representation of some decimal number. What
decimal number is being represented?
0 0 0 0 0 1 1 1 1 0 0 0 1 1 1 11
Question 16: By now you should realize that the signed magnitude representation and the ones complement
representation have identical bit patterns for positive numbers. To reinforce this, please fill in the table below.
Decimal 5 bit signed magnitude representation 5 bit ones complement representation
Value
15
14
13
12
3
You are a full time student.
Your job is to become a scholar.
You are on the varsity team.
Show up for practice.
Now is your time to be a student. Be a student.
Unit 4: Twos Complement Representation
91.101 Computing I
You need to know …
U4-1) how to convert a decimal number into its n-bit twos complement equivalent.
U4-2) how to convert an n-bit twos complement number into its decimal equivalent.
U4-3) what is the largest and smallest decimal number that can be represented as an n bit twos complement
number.
U4-4) that adding two twos complement numbers as if they were unsigned will always produced a correct result
and you can ignore the leftmost carry out.
U4-5) that a twos complement representation has one representation of zero.
Question 1:
Fill in the table below.
Decimal
Value
2 Bit Twos Complement
+1
0
-1
-2
Question 2: If you are given two bit positions what is the largest decimal number that could be represented
using a twos complement representation?
Question 3: If you are given two bit positions what is the smallest decimal number that could be represented
using a twos complement representation?
Question 4:
Fill in the table below.
Decimal Value
7
4 Bit twos Complement
6
5
4
3
2
1
0
-1
-2
-3
-4
-5
-6
-7
-8
Question 5: If you are given 4 bits, what is the largest decimal number that could be represented using a twos
complement representation?
Question 6: If you are given 4 bits, what is the smallest decimal number that could be represented using a twos
complement representation?
Question 7: Fill in the table below.
Number of Largest decimal number that could
Bits Given be represented in twos complement.
1
Smallest decimal number that could
be represented in twos complement.
2
3
4
5
6
7
8
9
10
11
12
Question 8: If you were given n bit positions what is the range of decimal values that can be represented using
a twos complement scheme? Your response should use n in an expression.
Question 9: Please represent the decimal number 5 as a four bit twos complement number.
Question 10: Please represent the decimal number -2 as a four bit twos complement number.
Question 11: Please add your answer to question 9 to your answer to question 10. Perform the addition as if
you were adding two unsigned numbers. If there is a leftmost carry out ignore it.
Question 12: Look at the result you obtained in the previous problem. Interpret these bits as if they were a twos
complement representation. What is the decimal equivalent of these bits if they are to be interpreted as a twos
complement number? Notice that your answer is 3. If you did not get 3, then you need to redo the problem.
Question 13: Please explain why twos complement is the preferred representation for integer values. Give me
two reasons (besides saving money).
Question 14: Suppose you have 12 bits to work with. Please represent the following decimal numbers using a
twos complement representation.
Decimal Value
500
12 Bit Twos Complement Number
-500
18
-18
Question 15: The following 16 bit pattern is a twos complement representation of some decimal number. What
decimal number is being represented?
0 0 0 0 0 1 1 1 1 0 0 0 1 1 1 11
Question 16: By now you should realize that the signed magnitude representation, the ones complement
representation, and the twos complement representation have identical bit patterns for positive numbers. To
reinforce this, please fill in the table below.
Decimal 5 bit signed magnitude
Value representation
5 bit ones complement
representation
5 bit twos complement
representation
15
14
13
12
3
Question 17: No matter how many bits are being used, decimal -1 is always represented as a bunch of 1s.
Please complete the table below.
Using 4 bits represent decimal -1 in twos
complement.
Using 8 bits represent decimal -1 in twos
complement.
Using 16 bits represent decimal -1 in twos
complement.
Unit 5: IEEE 754 Single Precision Floating Point Format
91.101 Computing I
You need to know …
U5-1) that the internal representation of the number 4.0 is different than the internal representation of the
number 4.
U5-2) how to convert a decimal number into IEEE 754 Single Precision Floating Point Format.
U5-3) how to convert an IEEE 754 Single Precision Floating Point number into decimal.
Basic Discussion
An IEEE 754 single precision floating point number is a 32 bit number with three fields. The fields are:
1 bit sign field
8 bit exponent field
23 bit fraction field
The combination of 32 bits represents either a normalized number, a denormalized number or a special case.
A normalized number has a mantissa that is between 1 and 2. The 1 to the left of the binary point is implicitly
assumed and it is not explicitly represented.
A denormalized number does not assume that an implicit 1 bit exists.
U5-2 Discussion
The following steps are taken when you convert a decimal number into its 32 bit IEEE 754 single precision
floating point representation.
Step 1: Form the binary.
Realize that we use the term decimal point when dealing with base ten numbers. We use the term binary point
when dealing with base two numbers.
To obtain the whole number part to the left of the binary point divide the decimal whole number part by 2,
record the remainder to the left of the binary point, and remember the quotient. Is the quotient 0? If not, then
we divide the quotient by 2 and repeat the process. This time record the remainder to the left of the last
remainder recorded. If the quotient is 0 then stop.
To obtain the fraction number part to the right of the binary point multiply the decimal fraction part by 2. If
there is an overflow carry of 1 out past the decimal point, record that 1 to the right of the binary point. If there is
no overflow, we record a zero to the right of the binary point. We continue this as long as the fraction part of the
decimal result is not zero. We may also stop if we run out of bits to fill.
Example.
How about the decimal number 5.125? Convert it into binary.
5 divided by 2
2 divided by 2
1 divided by 2
produces a quotient of 2
produces a quotient of 1
produces a quotient of 0
and produces a remainder of 1
and produces a remainder of 0
and produces a remainder of 1
We stop on the left since the quotient is 0. This produces 101.
Now what about the .125?
.125 times 2
.250 times 2
.500 times 2
produces 0.250 and record a 0 and remember the .250
produces 0.500 and record a 0 and remember the .500
produces 1.000 and record a 1 and remember the .000
Stop since the .000 has been reached.
We now have the binary equivalent of decimal 5.125. It is 101.001
Step 2: Normalize the number.
Get the result to become a number between 1 and 2 by sliding the binary point. Keep track of how many spaces
you’ve slid the binary point. If you make the number smaller it is a positive movement. If you make the number
larger it is a negative movement.
For example the number 101.001 requires a positive movement of +2 to get to 1.01001
Here are some other examples:
0.0011101 requires a negative movement of -3 to get to 1.1101
10000.111 requires a positive movement of 4 to get to 1.0000111
Step 3: Throw away the 1 and assume it is there.
The number 1 followed by the “ . ”followed by a bunch of 1s and 0s is called the normalized significand. Since
we force this number to be between 1 and 2, there is always a 1 to the left of the binary point in a normalized
significand. Since there is always a 1 in a normalized significand we do not need to store it.
We just drop it from the representation and assume it is there.
Step 4: Fill in the fraction field.
The IEEE 754 single precision standard has 23 bits to the right of the binary point so you need to pad to the
right with zeroes or you need to stop the multiply step after filling up 23 slots. Thus, the 23 bits to the right of
the binary point for the decimal number 5.125 is:
0100 1000 0000 0000 0000 000
Step 5: Fill in the exponent field.
The IEEE 754 standard uses a field of 8 bits for the exponent. Under normalized conditions, this is an excess
127 exponent. This 8 bit field is formed by adding the number 127 to the value of the movement found in step 2
and expressing the result as an unsigned binary integer.
For 101.001 there was a +2 movement, so you would get the number 129. Represent 129 using 8 bit unsigned
binary. Thus the exponent field for 5.125 is:
1000
0 001
If the 8 bit exponent field is all 0s (0000 0000) then the number does not represent a normalized number. It
either represents zero or a denormalized number. If the fraction field is all zeros then the numbers represents
zero. If the fraction field is non-zero, then it represents a denormalized number.
If the 8 bit exponent field is all 1s (1111 1111) the representation is that of a special case. The three special
cases are: positive infinity, negative infinity, and Not a Number.
Step 6: Fill in the sign field.
The IEEE 754 standard has one bit for the sign. Positive is zero. Negative is one. The sign bit for 5.125 is 0.
Step 7: Represent the entire number and smile.
SIGN BIT
5.125 is
0
EXPONENT
FRACTION
1000001 01001000000000000000000