Transcript 10/27/04

COMS 161
Introduction to Computing
Title: Numeric Processing
Date: October 27, 2004
Lecture Number: 25
1
Announcements
• Homework 7
– Due Wednesday, 11/03/2004
2
Review
• Numeric Processing
• Integers
– Magnitude representation
– Sign-magnitude representation
3
Outline
• Numeric Processing
• Integers
– Ones’s complement representation
– Two’s complement representation
• Real numbers
4
Sign-Magnitude
• Mathematical operations sometimes give
an incorrect result
4 – 3 = 4 + -3 = 1
4
+(-3)
1
0100
+1011
1 111
-7
5
Ones compliment
• Positive integers have the most significant bit
(leftmost) equal to 0
– The magnitude of positive numbers is the same as
the signed magnitude representation
• Negative integers have the most significant bit
equal to 1
– The magnitude of a negative number is the same
as the magnitude representation of a different
number
 N  2 1 N
n
6
Ones compliment
 N  2n  1  N
-3 = 23 – 1 – 3 = 8 – 4 = 4
-2 = 23 – 1 – 2 = 8 – 3 = 5
-1 = 23 – 1 – 1 = 8 – 2 = 6
-0 = 23 – 1 – 0 = 8 – 1 = 7
Unsigned
decimal
0
1
2
3
4
5
6
7
Ones
complement
000
001
010
011
100
101
110
111
Signed
decimal
0
1
2
3
-3
-2
-1
-0
7
Ones compliment
• Example: let n = 8, and N = 5
 N  2n  1  N
 5  2 8  1  5  256  6  250  1111 1010
– There is something special here and in the
previous table
5  0000 0101
- 5  1111 1010
– Negatives are made from the positive by inverting
each bit!
– Simpler hardware for arithmetic operations
– Still two representations of zero
8
Ones complement
Decimal Ones Complement
5
4
3
2
1
0
-0
-1
-2
-3
-4
-5
0000 0101
0000 0100
0000 0011
0000 0010
0000 0001
0000 0000
1111 1111
1111 1110
1111 1101
1111 1100
1111 1011
1111 1010
9
Ones complement
• Mathematical operations sometimes give
an incorrect result using this
representation
4 – 3 = 4 + -3 = 1
4
+(-3)
1
0100
+1100
00 00
4 – 2 = 4 + -2 = 2
0
4
+(-2)
2
• Two representations of zero
0100
+1101
00 01
1
10
Twos Complement
• Positive integers have the most significant bit
(leftmost) equal to 0
– The magnitude of positive numbers is the same as
signed magnitude and ones complement
representations
• Negative integers have the most significant bit
(leftmost) equal to 1
– Negative numbers can be computed as:
N 2 N
n
11
Two’s Complement
• Example: let n = 8, and N = 5
 N  2n  N
 5  2 8  5  256  5  251  1111 1011
– There is something special here
• ones’ complement  N  2  1  N
n

N

2
N
• two’s complement
• It is simple to determine the representation of a
negative number in ones complement given the positive
• It is easy to convert a ones complement representation
to a twos complement representation by simply adding
1
n
12
Twos Complement
Decimal Sign/mag
5
4
3
2
1
0
-0
-1
-2
-3
-4
-5
0000 0101
0000 0100
0000 0011
0000 0010
0000 0001
0000 0000
0000 0000
1000 0001
1000 0010
1000 0011
1000 0100
1000 0101
Ones’
Two’s
0000 0101
0000 0100
0000 0011
0000 0010
0000 0001
0000 0000
1111 1111
1111 1110
1111 1101
1111 1100
1111 1011
1111 1010
0000 0101
0000 0100
0000 0011
0000 0010
0000 0001
0000 0000
------------1111 1111
1111 1110
1111 1101
1111 1100
1111 1011
13