Warm-Up: Back To Elementary School Warm-Up: Back To Elementary School Warm-Up: Back To Elementary School
Warm-Up: Back To Elementary School Warm-Up: Back To Elementary School Warm-Up: Back To Elementary School
Warm-Up: Back To Elementary School Warm-Up: Back To Elementary School Warm-Up: Back To Elementary School
Warm-Up: Back to
Elementary School
What is the sum of each pair? And how Unsigned Number Systems
many times do you “carry the 1”? Number
• 26577 and 952 27529; 2 Systems
• 99645 and 468
• 637 and 203
The foundation upon which
• 99 and 80
Computer Architecture is built
For HW0 you ran one of: INPUT A 20,000 INPUT A
Java Program B = A + A B = A + A
import java.io.*;
public class hw0
{
OUTPUT B OUTPUT B
public static void main(String args[])
{
short a; //declare 'a'
a = 20000; //set to 2000
a = (short)(a + a); //double 'a'
System.out.println(a); //display 'a'
} C++ Program
}
#include <iostream>
int main()
{
short int a; //declare 'a'
a = 20000; //set to 20000
a = a + a; //double 'a'
std::cout << a << std::endl; //display 'a'
return (0); //exit
}
Most knew result too big for data size Most knew result too big for data size
That is not the
Which is fine Which is fine
significant part of the
BUT ... problem!
But that sort of accident would never But that sort of accident would never But that sort of accident would never
happen in the “Real World” happen in the “Real World” happen in the “Real World”
… right? … right?
1991 Patriot missile defense system in
Dharan, Saudi Arabia fails to track and
intercept an incoming Iraqi missile due to
an error in the software when converting
time to a 24 bit floating-point number.
28 Americans died when the missile hit
Dharan Air Base
https://en.wikipedia.org/wiki/List_of_software_bugs
http://www.cs.unc.edu/~smp/COMP205/LECTURES/ERROR/lec23/node4.html
But that was a long time ago ... Not all number systems
2015: U.S. Federal Aviation Authority requires Boeing 787 Dreamliner are amenable to
planes to be “rebooted” at least once every 248 days due to (likely) an XKCD
integer overflow issue that could trigger loss of power to the aircraft performing arithmetic
2016: A slot machine incorrectly prints out a winning ticket for Without using a calculator, how would
$42,949,672.76 … on a machine where the maximum winnings are
$10,000 you add:
• II and I?
April 20, 2020: Microsoft releases an out-of-band update for software that
uses AutoDesk FBX including Microsoft Office, Paint 3D to patch multiple • III and I?
vulnerabilities including an integer overflow vulnerability • XI and V?
https://www.engadget.com/2015-05-01-boeing-787-dreamliner-software-bug.html
https://en.wikipedia.org/wiki/Integer_overflow#Examples
https://www.desmoinesregister.com/story/news/crime-and-courts/2015/04/25/supreme-court-casino-
jackpot-denied/26356437/
https://www.autodesk.com/trust/security-advisories/adsk-sa-2020-0002
https://xkcd.com/1131/
https://portal.msrc.microsoft.com/en-us/security-guidance/advisory/ADV200004
Not all number systems Positional Number Systems Positional Number Systems
are amenable to
performing arithmetic Designed to allow straightforward rules Designed to allow straightforward rules
Without using a calculator, how would when performing arithmetic when performing arithmetic
you add:
Name Base Digits (0 thru base-1) Name Base Digits (0 thru base-1)
• II and I? II III XI
• III and I? Decimal 10 0-9
+ I + I + V
• XI and V? III IV XVI
https://commons.wikimedia.org/wiki/File:Two_hand,_ten_fingers.jpg
Set of positions represented by the base Set of positions represented by the base
raised to successively higher powers raised to successively higher powers Name Base Digits (0 thru base-1)
Decimal 10 0-9
179 = (1 x 102) + (7 x 101) + (9 x 100) 179 = (1 x 102) + (7 x 101) + (9 x 100)
179 = 100 + 70 + 9
10112 = (1 x 23)+(0 x 22)+(1 x 21)+(1 x 20) 10112 = (1 x 23)+(0 x 22)+(1 x 21)+(1 x 20)
Set of positions represented by the base Set of positions represented by the base
Digital computers use base 2 raised to successively higher powers raised to successively higher powers
A number is represented as the sum of the
positional value times a digit
Base 2 Rule for adding PNS digits Rule for adding PNS digits
Name Base Digits (0 thru base-1)
If the sum of the digits is less than the If the sum of the digits is less than the
Decimal 10 0-9 base then the result is represented base then the result is represented
Binary 2 0-1 by a single digit by a single digit
If the sum of the digits is less than the If the sum of the digits is less than the If the sum of the digits is less than the
base then the result is represented base then the result is represented base then the result is represented
by a single digit by a single digit by a single digit
11
26577 This rule is very basic for
Else (the sum is equal or greater than Else (the sum is equal or greater than Else (the sum is equal or greater than
+ 952 unsigned numbers
the base) then the result is reduced the base) then the result is reduced the base) then the result is reduced
27529
by the base and represented by a by the base and represented by a by the base and represented by a
single digit and a carry into the next single digit and a carry into the next single digit and a carry into the next
higher position higher position higher position
Rule for adding PNS digits Rule for adding PNS digits Rule for adding PNS digits
If the sum of the digits is less than the If the sum of the digits is less than the If the sum of the digits is less than the
base then the result is represented base then the result is represented base then the result is represented
by a single digit by a single digit by a single digit
This rule Key to the most important
becomes critical
Else (the sum is equal or greater than
decision to be made by
Else (the sum is equal or greater than
source of many security
Else (the sum is equal or greater than
when then
we address signed the designer of a new vulnerabilities
the base) the result is reduced the base) then the result is reduced the base) then the result is reduced
numbers computer
by the base and represented by a by the base and represented by a by the base and represented by a
single digit and a carry into the next single digit and a carry into the next single digit and a carry into the next
higher position higher position higher position
Decimal (ten) carry into
Decimal next position Decimal
1
7 7 7 7 7 7
two are
+ 2 + 5 + 2 + 5 left + 2 + 5
9 9 12 9 12
0 0 1 1 0 0 1 1 0 0 1 1
+ 0 + 1 + 0 + 1 + 0 + 1 + 0 + 1 + 0 + 1 + 0 + 1
0 1 1
Decimal
Apply the Rule to Apply the Rule to
Multiple Digits Multiple Digits
7 7
+ 2 + 5 Did this in the warm-up with Base-10
9 12
carry into 10112 =
next
11
Binary position 26577 1110
1 + 952 + 10112 =
0 0 1 1 27529 1110
+ 0 + 1 + 0 + 1
0 1 1 10
Apply the Rule to Apply the Rule to Apply the Rule to
Multiple Digits Multiple Digits Multiple Digits
1 11
10112 = 10112 = 10112 =
1110 1110 1110
+ 10112 = + 10112 = + 10112 =
1110 1110 1110
02 102
2210 2210 2210
11 1 11 1 11
10112 = 10112 = 10112 =
1110 1110 1110
+ 10112 = + 10112 = + 10112 =
1110 1110 1110
1102 101102 Are these the
10110 = same?
2
2210 2210 2210
Verify our result Verify our result Verify our result
? = 101102 ? = 101102
? = 101102
? = (1 x 24)+(0 x 23)+(1 x 22)+(1 x 21)+(0 x 20) ? = (1 x 24)+(0 x 23)+(1 x 22)+(1 x 21)+(0 x 20)
2210= 16 + 0 + 4 + 2 + 0
Expand to true positional format
Do the math
1 0 1 1 0
16 8 4 2 1
Conversion of Binary to Decimal Conversion of Binary to Decimal Conversion of Decimal to Binary
Expansion of Powers Expansion of Powers Reduction of Powers
4 3 2 1 4 3 2 1
101102=(1 x 2 )+(0 x 2 )+(1 x 2 )+(1 x 2 )+(0 x 101102=(1 x 2 )+(0 x 2 )+(1 x 2 )+(1 x 2 )+(0 x 2210 =
20) 20)
(_x25)+(_x24)+(_x23)+(_x22)+(_x21)+(_x20
101102= 16 + 4 + 2 101102= 16 + 4 + 2 )
101102= 2210 101102= 2210
or ... just write the positional values or ... just write the positional values
below the digits and do the math below the digits and do the math
1 0 1 1 0 1 0 1 1 0
16 8 4 2 1 16 8 4 2 1
= 22
Subtraction Subtraction
Subtraction Subtraction Subtraction
How do computers subtract? How do computers subtract? How do computers subtract?
HLL Source Machine Hardware HLL Source Machine Hardware HLL Source Machine Hardware
(e.g. Java, C++) Code (e.g. Java, C++) Code (e.g. Java, C++) Code
C=A-B; SUB A,B,C C=A+(-B) C=A-B; SUB A,B,C C=A+(-B) C=A-B; SUB A,B,C C=A+(-B)
Why? Why?
Do not need special
Addition Addition Addition
subtract hardware
Complement Complement Complement
Arithmetic Arithmetic Arithmetic
85 85 85
-07 -07 Re-write in true -07
Long way positional form
All the steps
( 8x101) + ( 5x100) ( 8x101) + ( 5x100)
Then we will -( 0x101) + ( 7x100) -( 0x101) + ( 7x100)
see a clever
shortcut
https://flickr.com/photos/67193564@N03/6173548853
Decimal Subtraction Decimal Subtraction Decimal Subtraction
85 85 85
-07 -07 -07
85 85
-07 -07
78
Binary Subtraction Binary Subtraction Binary Subtraction
1002 = 410 1002 = 410 1002 = 410
- 0012 = 110 - 0012 = 110 - 0012 = 110
Need to redistribute
Re-write in true positional form data to continue
Increases accuracy
(helps when working in different bases)
1 1
0 1 0 1
2210 = 1 0 1 1 02 2210 = 1 0 1 1 02 2210 = 1 0 1 1 02
-1110 = -0 1 0 1 12 -1110 = -0 1 0 1 12 -1110 = -0 1 0 1 12
1
This requires
redistribution
of data
Can we speed up subtraction? Can we speed up subtraction? Can we speed up subtraction?
1 1 1
1 1 1 1 1 1
0 0 1 0 0 1 0 0 1
2210 = 1 0 1 1 02 2210 = 1 0 1 1 02 2210 = 1 0 1 1 02
-1110 = -0 1 0 1 12 -1110 = -0 1 0 1 12 -1110 = -0 1 0 1 12
1 1 1 0 1 1
1 1 1
1 1 1 1 1 1 1 1 1
0 1 0 0 1 0 1 0 0 1 0 1 0 0 1
2210 = 1 0 1 1 02 2210 = 1 0 1 1 02 2210 = 1 0 1 1 02
-1110 = -0 1 0 1 12 -1110 = -0 1 0 1 12 -1110 = -0 1 0 1 12
0 1 1 1 0 1 1 0 1 0 1 1
Can we speed up subtraction? Can we speed up subtraction? Concepts so far
1 1
❏ Addition
1 1 1 1 1 1
0 1 0 0 1 0 1 0 0 1 ❏ Positional Number Systems
2210 = 1 0 1 1 02 2210 = 1 0 1 1 02
❏ Conversion Among Bases
-1110 = -0 1 0 1 12 -1110 = -0 1 0 1 12
1110 = 0 1 0 1 12 1110 = 0 1 0 1 12 ❏ Subtraction
Long binary numbers are hard for Long binary numbers are hard for
humans to remember humans to remember Shortcut to working in binary
The binary The binary
value of PI is Darn ... I can value of PI is 1 0 1 1 0 1 1 0 1 1 0 0 0 1 1 12
11.0100110101 only remember 11.0100110101
01010101001... 7 digits 01010101001...
Shortcut to working in binary Shortcut to working in binary Concepts so far
1 0 1 1 0 1 1 0 1 1 0 0 0 1 1 12 1 0 1 1 0 1 1 0 1 1 0 0 0 1 1 12
❏ Addition
C 5 E
Do math in decimal
when converting to
decimal
Hex → Decimal Hex → Decimal Decimal → Hex
Expansion of Powers to get Decimal Expansion of Powers to get Decimal Reduction of Powers to get Hexadecimal
A16
7 A16 7 A16 7 A16
+ E16
+ 5 E16 + 5 E16 + 5 E16
Addition in Hex Addition in Hex Addition in Hex
Greater than
the base (16)
1 1
A16 = 1010 A16 = 1010
7 A16 7 A16 7 A16
+ E16 = 1410 + E16 = 1410
+ 5 E16 + 5 E16 + 5 E16
24 = 16 + 8 24 = 16 + 8
8 8
Greater than Greater than
the base (16) the base (16)
Addition in Hex Addition in Hex Addition in Hex
1 1 1 1 1 1 1 1
716 7 A16 716 = 710 7 A16 716 = 710 7 A16
+ 516 + 5 E16 + 516 = 510 + 5 E16 + 516 = 510 + 5 E16
8 8 13 8
1 1 1 1 1 1 1
716 = 710 7 A16 716 = 710 7 A16 7 A16
+ 516 = 510 + 5 E16 + 516 = 510 + 5 E16 + 5 E16
13 8 13 D 8 D 816
C 10 1016 C 10 1016 C 10
D 816 + 816 D 816 1610 D 816
- 5 E16 - E16 - 5 E16 + 816 = + - 5 E16
Each 161=16*160 810
- E16 =
-1410
Subtraction in Hex Subtraction in Hex Subtraction in Hex
1016 C 10 C 10 C 10
D 816 D 816 C16 D 816
1610
- 5 E16 - 5 E16 - 516 - 5 E16
+ 816 = +
810 A A A
- E16 =
-1410
10
C 10 C 10
C16 = 1210 D 816 C16 = 1210 D 816 D 816
- 516 = - 510 - 5 E16 - 516 = - 510 - 5 E16 - 5 E16
A 710 7 A 7 A
Concepts so far
Overflow
❏ Addition
❏ Positional Number Systems 9
+ 2
❏ Conversion Among Bases
❏ Subtraction
❏ Hexadecimal
Working with pencil and paper,
❏ Overflow dry-erase markers and whiteboard,
etc.
1 1 1
9 9 9
+ 2 + 2 + 2
1 11 11
Working with pencil and paper, Working with pencil and paper, Working with pencil and paper,
dry-erase markers and whiteboard, dry-erase markers and whiteboard, dry-erase markers and whiteboard,
etc. etc. etc.
Finite Precision Arithmetic
Digits are fixed by the Manufacturer Digits are fixed by the Manufacturer
Once you build hardware Once you build hardware Once you build hardware
11 11
1101 0001 1101 0001 1101 0001
+ 0100 - 0100 + 0100 - 0100 + 0100 - 0100
13 + 4 1-4 0001 0001
Desired result = 17 Desired result = -3 Result
presented
17 > 15 which is the -3 < 0 which is the
largest value that smallest value that
fits in a 4 bit fits in a 4 bit
unsigned number unsigned number
Examples using 4 bit Examples using 4 bit Examples using 4 bit
unsigned numbers unsigned numbers unsigned numbers
Addition Subtraction Addition Subtraction Addition Subtraction
1 1
11 11 011 11 011
1101 0001 1101 10001 1101 10001
+ 0100 - 0100 + 0100 - 0100 + 0100 - 0100
0001 0001 1101 0001 1101
Result Result
presented presented
Overflow detected
by carry out of the
Most Significant
Digit (MSD)