Professional Documents
Culture Documents
Signed Number Systems: Java in A Nutshell
Signed Number Systems: Java in A Nutshell
Signed Number Systems: Java in A Nutshell
CSC 236
Architectural design
● Previously: Showed unsigned positional number (UPN)
7 5
● Need to handle signed numbers as well
● Critical architectural question
○ Does signed number system use same addition rule as UPN
Add Add hardware
12
7 5 -7 5
7 5 -7 5
Add
Use the same rule for addition in signed number system as in UPN? Does our “everyday” signed magnitude follow the addition
rule for UPN?
No right or wrong answer
Yes No
Does our “everyday” signed magnitude follow the addition Does our “everyday” signed magnitude follow the addition
rule for UPN? rule for UPN?
10 +10 10 +10
+ 05 + -05 + 05 + -05
15 15 5
Does our “everyday” signed magnitude follow the addition Does our “everyday” signed magnitude follow the addition
rule for UPN? rule for UPN?
10 +10 10 +10
+ 05 + -05 + 05 + -05
15 15 15 ?15
Does our “everyday” signed magnitude follow the addition Can you do
rule for UPN? arithmetic Yes. You
on Signed extend the
Magnitude UPN rule.
numbers?
Unsigned Signed Magnitude
10 +10
+ 05 + -05
15 The rule does not
?15
handle this column
Extend the UPN rule to handle Signed Magnitude Extend the UPN rule to handle Signed Magnitude
+10 +10 -10 -10 +10 +10 +10 -10 -10 +10
+05 -05 +05 -05 -10 +05 -05 +05 -05 -10
+15 +05 -05 -15 ±00 +15 +05 -05 -15 ±00
Extend the UPN rule to handle Signed Magnitude Extend the UPN rule to handle Signed Magnitude
+10 +10 -10 -10 +10 +10 +10 -10 -10 +10
+05 -05 +05 -05 -10 +05 -05 +05 -05 -10
+15 +05 -05 -15 ±00 +15 +05 -05 -15 ±00
Extend the UPN rule to handle Signed Magnitude Extend the UPN rule to handle Signed Magnitude
+10 +10 -10 -10 +10 +10 +10 -10 -10 +10
+05 -05 +05 -05 -10 +05 -05 +05 -05 -10
+15 +05 -05 -15 ±00 +15 +05 -05 -15 ±00
Extend the UPN rule to handle Signed Magnitude Extend the UPN rule to handle Signed Magnitude
+10 +10 -10 -10 +10 +10 +10 -10 -10 +10
+05 -05 +05 -05 -10 +05 -05 +05 -05 -10
+15 +05 -05 -15 ±00 +15 +05 -05 -15 ±00
+10 +10 -10 -10 +10 +10 +10 -10 -10 +10
+05 -05 +05 -05 -10 +05 -05 +05 -05 -10
+15 +05 -05 -15 ±00 +15 +05 -05 -15 ±00
Question
?
+0 & -0 dollars in you
wallet is the same.
+0 = -0
Told that launch There are multiple ways to represent signed numbers
authorization code is:
Will present three different signed number systems
zero.
● Signed magnitude
The program checks: ● One’s complement
● Two’s complement
if input == code
Has computer been built using Signed Magnitude? Has computer been built using Signed Magnitude?
IBM 7090 -- circa 1958
Another example
- 1 = 1 1 1 0
- 1 = 1 1 1 0
- 2
1 1 1 1 1 1
- 1 = 1 1 1 0 - 1 = 1 1 1 0
- 1 = 1 1 1 0 - 1 = 1 1 1 0
- 2 1 1 0 0 - 2 1 1 0 0 0 0 1 1 = +3
Does this equal -2? Flip bits
Another example One’s complement
Need to extend the UPN rule for one’s complement
1 1 1
- 1 = 1 1 1 0 “End around carry”
- 1 = 1 1 1 0 1 1 1
- 2 1 1 0 0 0 0 1 1 = +3 - 1 = 1 1 1 0
- 1 = 1 1 1 0
- 2 1 1 0 0 A carry out of the left
-3 1 most digit is added to
the right most digit
1 1 0 1
Relative quantity of
positive and negative Same Same
values
Has computer been built using One’s complement? Has computer been built using One’s complement?
Can we add using the UPN rule? Can we add using the UPN rule?
-1 + (-1) failed for one’s complement What about zero? How many representations?
Two’s complement Two’s complement
Only one zero leads to quirk.
bin dec dec bin 000
0000 +0 -0 —-- +0 = 0000 +0
0001 +1 -1 1111 111 +1 001
0010 +2 -2 1110 1111
0011 +3 -3 1101 TC(0) = 1111 110 +2 010
0100 +4 -4 1100 + 1
0000 101 +3 011
Has computer been built using two’s complement? Has computer been built using two’s complement?
Most modern computers use two’s complement
Overflow Overflow
1. Two numbers with like signs and a different results 1. Two numbers with like signs and a different results
000
The sum of positive and negative has a magnitude smaller than
+0
the largest ⇒ magnitude decreases
1 111 -1 +1 001
+2 = 010
110 -2 +2 010
+2 = 010
100 101 -3 +3 011
-4
sign change 100
Overflow Overflow
1. Two numbers with like signs and a different results 1. Two numbers with like signs and a different results
2. If carry-out of sign ≠ carry-in an overflow occurred
The sum of positive and negative has a magnitude smaller than
the largest ⇒ magnitude decreases
A-B=A+B+1 A-B=A+B+1
0100
-0101
1111
What’s the big idea? Part of a C program
Two’s Complement does not need separate hardware for unsigned U = 10; C chooses to treat the test as unsigned.
subtraction!
if (U > -1) printf ("U greater than -1");
With Two’s Complement
the same hardware add if (U < -1) printf ("U less than -1 ");
A-B=A+B+1 instruction works for
● unsigned or U less than -1
0100 0100 ● signed
-0101 +1010 numbers 1010 = 000A16 and -110 =
1111 1 FFFF16
1111 Program compared an unsigned number (10) to a signed number.
No hardware support for that.