Signed Number Systems: Java in A Nutshell

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Signed number systems

CSC 236

Errors will occur Java in a Nutshell (Flanagan)


“Integer arithmetic in Java is modular which means that it never
produces an overflow when you exceed the range of a given
integer type. Instead numbers just wrap around.”

We need to prepare for them

Java in a Nutshell (Flanagan) Java in a Nutshell (Flanagan)


“Integer arithmetic in Java is modular which means that it never “Integer arithmetic in Java is modular which means that it never
produces an overflow when you exceed the range of a given produces an overflow when you exceed the range of a given
integer type. Instead numbers just wrap around.” integer type. Instead numbers just wrap around.”

When doing integer arithmetic, you


Neither the Java compiler nor Java VM warns you in any way when Neither the Java compilersimply
nor Javamust ensurewarns
Interpreter that the
youtype you
in any
this occurs. way when this occurs. are using has sufficient range for the
purpose you intend.
New plan: New plan:

Don’t put bugs


in your Java
Code.

Part of a C program Part of a C program


unsigned U = 10; unsigned U = 10;
e.
if (U > -1) printf ("U greater than -1"); tur than -1");
if (U > -1) printf ("U greater
c
if (U < -1) printf ("U less than -1 "); if (U < -1) printf ("U s le
hi less than -1 ");
t
d in
ine
Which message is desired? pla Which message is printed?
Ex
C thinks 10 < -1

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

Add Add hardware -2


7 5 -7 5

Use the same rule for UnAdd Different SiAdd


-2 hardware
addition in signed number
system as in UPN? 12 -2

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

● 1 instructions ● Can optimize performance of Unsigned Signed Magnitude


● Less hardware both signed and unsigned
● Less cost instructions 10 +10
+ 05 + -05
5

Does our “everyday” signed magnitude follow the addition Does our “everyday” signed magnitude follow the addition
rule for UPN? rule for UPN?

Unsigned Signed Magnitude Unsigned Signed Magnitude

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?

Unsigned Signed Magnitude Unsigned Signed Magnitude

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

If signs are the same


● Add digits using UPN rule
● Result has sign of input numbers
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

Else if magnitudes are different Else


● Subtract smaller from larger ● Result is zero
● Result has sign of larger ● Sign is plus or minus

Extend the UPN rule to handle Signed Magnitude Signed numbers

+10 +10 -10 -10 +10 Use 4 bits d d d d


+05 -05 +05 -05 -10
leftmost bit is sign other bits provide
+15 +05 -05 -15 ±00 0 = pos 1 = neg magnitude

Do you want to build such a We will see …


complicated rule into hardware? ● Positive numbers look the same but
● Negative number differ

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

Characteristics of interest Signed magnitude


1. Rule for addition
2. How many ways can you represent zero bin dec dec bin
3. Relative quantity of positive and negative values 0000 +0
0001 +1
0010 +2
0011 +3
0100 +4

Signed magnitude Signed magnitude

bin dec dec bin bin dec dec bin


Add numbers
0000 +0 -0 1000 0000 +0 -0 1000
0001 +1 -1 1001 0001 +1 -1 1001 + 1 = 0001
0010 +2 -2 1010 0010 +2 -2 1010 - 1 = 1001
0011 +3 -3 1011 0011 +3 -3 1011 +/- 0
0100 +4 -4 1100 0100 +4 -4 1100

Negate obtained by inverting sign bit


Signed magnitude Characteristics of interest
Signed
magnitude

bin dec dec bin Same rule for addition


Add numbers No
0000 +0 -0 1000 1
0001 +1 -1 1001 + 1 = 0001 How many ways can
0010 +2 -2 1010 - 1 = 1001 you represent zero 2

0011 +3 -3 1011 +/- 0 1010


0100 +4 -4 1100 Relative quantity of
To add binary signed magnitude positive and negative
numbers, must extend UPN rule Same

(as for decimal) values

Has computer been built using Signed Magnitude? Has computer been built using Signed Magnitude?
IBM 7090 -- circa 1958

Intended for the scientific community and


NASA's Mercury and Gemini space missions

One’s complement One’s complement

bin dec dec bin bin dec dec bin


0000 +0 0000 +0 -0 1111
0001 +1 0001 +1 -1 1110
0010 +2 0010 +2 -2 1101
0011 +3 0011 +3 -3 1100
0100 +4 0100 +4 -4 1011

Negate obtained by inverting all bits


One’s complement One’s complement

bin dec dec bin bin dec dec bin


Add numbers Add numbers
0000 +0 -0 1111 0000 +0 -0 1111
0001 +1 -1 1110 + 1 = 0001 0001 +1 -1 1110 + 1 = 0001
0010 +2 -2 1101 - 1 = 1110 0010 +2 -2 1101 - 1 = 1110
0011 +3 -3 1100 +/- 0 0011 +3 -3 1100 +/- 0 1111
0100 +4 -4 1011 0100 +4 -4 1011
Equals -0 Looks okay

Another example

- 1 = 1 1 1 0
- 1 = 1 1 1 0
- 2

Another example Another example

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

Characteristics of interest Has computer been built using One’s complement?


Signed One’s
magnitude complement

Same rule for addition No


No
(simple rule)

How many ways can


2 2
you represent zero

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?

Univac 1100 - 1962


DEC PDP-1 - 1960
Main frame
First mini-computer
Two’s complement Two’s complement
N + (-N) = 0
Definition N + TC(N) = 2d ● 20 = 1
N + TC(N) = 2 d ● 21 = 10
N + (-N) =0
● 22 = 100
N = any number ● 23 = 1000
TC(N) = -N ● …
d = number of digits used
1 0 0 0 0 0 0 0 ● 2d = 10…0
d zeros ● 2d = 1 followed by d 0s
carry + zero

How to calculate two’s complement Two’s complement


d
N + TC(N) = 2
bin dec dec bin
Rewrite
0000 +0
TC(N) = 2d - N 0001 +1
0010 +2
For d = 4
0011 +3
TC(N) = 10000 - N 0100 +4

Two’s complement TC(N) = 10000 - N Two’s complement TC(N) = 10000 - N

bin dec dec bin bin dec dec bin


0000 +0 -0 0000 +0 -0
0001 +1 -1 0001 +1 -1
0010 +2 -2 0010 +2 -2
0011 +3 -3 0011 +3 -3 10000
0100 +4 -4 0100 +4 -4 - 0001
Two’s complement TC(N) = 10000 - N Two’s complement TC(N) = 10000 - N

bin dec dec bin bin dec dec bin


0000 +0 -0 0000 +0 -0
0001 +1 -1 1 0001 +1 -1 1
0010 +2 -2 01111 redistribute the 0010 +2 -2 01111
0011 +3 -3 10000 data 0011 +3 -3 10000
0100 +4 -4 - 0001 0100 +4 -4 - 0001
1111

Two’s complement TC(N) = 10000 - N Two’s complement TC(N) = 10000 - N

bin dec dec bin bin dec dec bin


0000 +0 -0 0000 +0 -0
0001 +1 -1 1111 1 0001 +1 -1 1111
0010 +2 -2 01111 0010 +2 -2
0011 +3 -3 10000 0011 +3 -3 10000
0100 +4 -4 - 0001 0100 +4 -4 - 0010
1111

Two’s complement TC(N) = 10000 - N Two’s complement TC(N) = 10000 - N

bin dec dec bin bin dec dec bin


0000 +0 -0 0000 +0 -0
0001 +1 -1 1111 0001 +1 -1 1111 1
0010 +2 -2 0010 +2 -2 0111
0011 +3 -3 10000 0011 +3 -3 10000
0100 +4 -4 - 0010 0100 +4 -4 - 0010
0 0
Two’s complement TC(N) = 10000 - N Two’s complement TC(N) = 10000 - N

bin dec dec bin bin dec dec bin


0000 +0 -0 0000 +0 -0
0001 +1 -1 1111 1 0001 +1 -1 1111 1
0010 +2 -2 0111 0010 +2 -2 1110 0111
0011 +3 -3 10000 0011 +3 -3 10000
0100 +4 -4 - 0010 0100 +4 -4 - 0010
1110 1110

Is there an Two’s complement shortcut


easier way to Yes.
calculate There is a TC(N) = flip-bits + 1
two’s shortcut
complement?

Two’s complement TC(N) = flip=bits + 1 Two’s complement TC(N) = flip=bits + 1

bin dec dec bin bin dec dec bin


0000 +0 -0 0000 +0 -0
0001 +1 -1 1111 +3 = 0011 0001 +1 -1 1111 +3 = 0011
0010 +2 -2 1110 0010 +2 -2 1110
0011 +3 -3 -3 = 1100 0011 +3 -3 1101 -3 = 1100
0100 +4 -4 + 1 0100 +4 -4 + 1
1101 1101
Two’s complement TC(N) = flip=bits + 1 Two’s complement TC(N) = flip=bits + 1

bin dec dec bin bin dec dec bin


0000 +0 -0 0000 +0 -0
0001 +1 -1 1111 +4 = 0100 0001 +1 -1 1111 -2 = 1110
0010 +2 -2 1110 0010 +2 -2 1110
0011 +3 -3 1101 -4 = 1011 0011 +3 -3 1101 +2 = 0001
0100 +4 -4 1100 + 1 0100 +4 -4 1100 + 1
1100 0010
Using high school algebra can show that:
flip-bits + 1 = 2d - N

Two’s complement Two’s complement

bin dec dec bin bin dec dec bin


0000 +0 -0 0000 +0 -0 1111
0001 +1 -1 1111 +1 = 0001 0001 +1 -1 1111 +1 = 0001
0010 +2 -2 1110 -1 = 1111 0010 +2 -2 1110 -1 = 1111
0011 +3 -3 1101 +0 = 0011 +3 -3 1101 +0 = 0000
0100 +4 -4 1100 0100 +4 -4 1100

Can we add using the UPN rule? Can we add using the UPN rule?

Two’s complement Two’s complement

bin dec dec bin bin dec dec bin


0000 +0 -0 0000 +0 -0 +0 = 0000
1111
0001 +1 -1 1111 -1 = 1111 0001 +1 -1 1111
0010 +2 -2 1110 -1 = 1111 0010 +2 -2 1110
TC(0) = 1111
0011 +3 -3 1101 -2 = 1110 0011 +3 -3 1101
+ 1
0100 +4 -4 1100 0100 +4 -4 1100

-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

What about zero? How many representations? 100

Two’s complement Two’s complement


Only one zero leads to quirk. Only one zero leads to quirk.
000 000
+0 +0
111 -1 +1 001 Quantity of positive and 111 -1 +1 001
negative values differ
110 -2 +2 010 110 -2 +2 010

101 -3 +3 011 101 -3 +3 011


-4 -4
100 100

Two’s complement Two’s complement


Can we verify the number wheel? For 1, 2, 3 we can complement
000 the value to show its negative 000
0 - 1 should be -1
+0 +0
Logically 111 -1 +1 001 111 -1 +1 001

000 110 -2 +2 010 110 -2 +2 010


- 001
111 101 -3 +3 011 101 -3 +3 011
-4 -4
100 100
Two’s complement Characteristics of interest
Signed One’s Two’s
For -4, we need a different strategy magnitude complement complement

000 Same rule for addition No


No Yes
+0 (simple rule)

-2 = 110 111 -1 +1 001


How many ways can
+ -2 = +110 you represent zero 2 2 1
110 -2 +2 010
-4 100
101 -3 +3 011 Relative quantity of
Recall a byte Java ranges from -4 positive and negative One more
-128 to +127 100
Same Same
negative
values

Has computer been built using two’s complement? Has computer been built using two’s complement?
Most modern computers use two’s complement

Overflow Two ways to detect


Two’s complement is 1. Intuitive
2. Mathematical
finite precision arithmetic

Therefore it can overflow


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 2 positive numbers should be positive.
+0
The sum of 2 negative numbers should be negative. 111 -1 +1 001
+2 = 010
110 -2 +2 010
+2 = 010
101 -3 +3 011
-4
100

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

Can be expressed mathematically. Look at sign bit column


0 1
2. If carry-out of sign ≠ carry-in an overflow occurred +2 = 0 1 0
+2 = 0 1 0
1 0 0
Two’s complement in Hex Two’s complement in Hex
11
Is this correct?
05 05
+FF +FF
04

Two’s complement in Hex Two’s complement in Hex


11 11
hex binary sign hex binary sign
05 0-7 0000-0111 pos 05 = +5 0-7 0000-0111 pos
+FF 8-F 1000-1111 neg +FF = -1 8-F 1000-1111 neg
04 04 +4

1111 1111 --> 0000 0000


1
0000 0001 --> +1

What’s the big idea? What’s the big idea?


Two’s Complement does not need separate hardware for Two’s Complement does not need separate hardware for
subtraction! subtraction!

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.

You might also like