You are on page 1of 39

Warm-Up: Back to Warm-Up: Back to Warm-Up: Back to

Elementary School Elementary School Elementary School


Without using a calculator add: Without using a calculator, how would Without using a calculator, how would
• 26577 and 952 you add 26577 and 952? you add 26577 and 952?
• 99645 and 468
• 637 and 203
• 99 and 80
26577
+ 952

Warm-Up: Back to Warm-Up: Back to Warm-Up: Back to


Elementary School Elementary School Elementary School
Without using a calculator, how would Without using a calculator, how would Without using a calculator, how would
you add 26577 and 952? you add 26577 and 952? you add 26577 and 952?
1 11
26577 26577 26577
+ 952 + 952 + 952
9 29 529
Warm-Up: Back to Warm-Up: Back to Warm-Up: Back to
Elementary School Elementary School Elementary School
Without using a calculator, how would Without using a calculator, how would Without using a calculator add:
you add 26577 and 952? you add 26577 and 952? • 26577 and 952
• 99645 and 468
11 11
• 637 and 203
26577 26577 • 99 and 80
+ 952 + 952
7529 27529

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
}

20,000 INPUT A 20,000 INPUT A 20,000 INPUT A


B = A + A B = A + A B = A + A
OUTPUT B OUTPUT B OUTPUT B

Expected: Expected: Actual: Expected: Actual:


40,000 40,000 -25,536 40,000 -25,536

Most knew result too big for data size


20,000 INPUT A 20,000 INPUT A 20,000 INPUT A
B = A + A B = A + A B = A + A
OUTPUT B OUTPUT B OUTPUT B

Expected: Actual: Expected: Actual: Expected: Actual:


40,000 -25,536 40,000 -25,536 40,000 -25,536

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!

20,000 INPUT A The application The application


B = A + A
Missile Control Missile Control
OUTPUT B
Should be Missile is launched.
A = minimum height
40,000 feet Reaches height A (20,000 ft)
2A = optimal height
Expected: Actual: Software says go to 2A
2A
40,000 -25,536

Runtime Systems did not A 20,000


provide a warning or error, they
just output the wrong answer.
The application The application The application
Missile Control Missile Control Missile Control

Missile is launched. Missile is launched. Missile is launched.


Reaches height A (20,000 ft) Reaches height A (20,000 ft) Reaches height A (20,000 ft)
Software says go to 2A Software says go to 2A Software says go to 2A
2*A Missile turns down 2*A Missile turns down
Instead is
...
-25,536
20,000 A A

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

Common Set of Characteristics Common Set of Characteristics


Positional Number Systems Positional Number Systems Positional Number Systems
Humans tend to use base 10. Why? Set of positions represented by the base
Name Base Digits (0 thru base-1) raised to successively higher powers
Decimal 10 0-9
102 101 100

https://commons.wikimedia.org/wiki/File:Two_hand,_ten_fingers.jpg

Humans tend to use base 10. Why? Pre-Columbian Mesoamerican cultures


such as the Maya used a base-20 system
(using all twenty fingers and toes)

Positional Number Systems Positional Number Systems Positional Number Systems

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

A number is represented as the sum of What do computers use?


the positional value times a digit
value = digit * position value
Two Types of Computer
Two Types of Computer Two Types of Computer
Analog Computer
4
Digital Digital Heathkit Analog 3
Computer Computer Computer circa 1956 2
1
0
~2.5 to ~3.5 volts is 3

Uses Voltage Level to


represent number

Two Types of Computer Two Types of Computer Two Types of Computer


Analog Computer Digital Computer Analog Computer Digital Computer Analog Computer
4 1 4 1 4
Error Prone 3 3 3
2 More 2 More 2
Reliable Reliable
1 1 1
Being a little bit off 0 0 0 0 0
gives the wrong ~2.5 to ~3.5 volts is 3 ~2.5 to ~3.5 volts is 3 ~2.5 to ~3.5 volts is 3
Full Range Full Range
number
Uses Voltage Level to Uses Voltage On/Off to Uses Voltage Level to Uses Voltage On/Off to Uses Voltage Level to
represent number represent a number represent number represent a number represent number
Digital Computers only need to
represent two values: 0 and 1
Base 2 Base 2 Base 2
Name Base Digits (0 thru base-1) Name Base Digits (0 thru base-1) Name Base Digits (0 thru base-1)
Decimal 10 0-9 Decimal 10 0-9 Decimal 10 0-9
Binary 2 0-1 Binary 2 0-1 Binary 2 0-1

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

10112 = (1 x 23)+(0 x 22)+(1 x 21)+(1 x 20)


Else (the sum is equal or greater than Else (the sum is equal or greater than
the base) then the result is reduced the base) then the result is reduced
Do the math in decimal to
by the base and represented by a by the base and represented by a
get the decimal value
single digit and a carry into the next single digit and a carry into the next
10112 = 8 + 0 + 2 + 1 = 1110
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
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

Binary Binary Binary

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

Apply the Rule to Apply the Rule to Apply the Rule to


Multiple Digits Multiple Digits Multiple Digits

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

Concepts so far Conversion of Binary to Decimal Conversion of Binary to Decimal


Expansion of Powers Expansion of Powers
❏ Addition 101102=(1 x 24)+(0 x 23)+(1 x 22)+(1 x 21)+(0 x 101102=(1 x 24)+(0 x 23)+(1 x 22)+(1 x 21)+(0 x
20) 20)
❏ Positional Number Systems
101102= 16 + 4 + 2 101102= 16 + 4 + 2
❏ Conversion Among Bases 101102= 2210 101102= 2210
or ... just write the positional values
Full Expansion
below the digits

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

Conversion of Decimal to Binary Conversion of Decimal to Binary Conversion of Decimal to Binary


Reduction of Powers Reduction of Powers Reduction of Powers
2210 = 2210 = 2210 =
(_x25)+(_x24)+(_x23)+(_x22)+(_x21)+(_x20 (0x25)+(_x24)+(_x23)+(_x22)+(_x21)+(_x20 (0x25)+(1x24)+(_x23)+(_x22)+(_x21)+(_x20
) ) )
25=32 22 24=16 So the digit is 1
-16 16<22
32>22 So the digit is 0 06
Need to know the
largest power of 2 in 22
Conversion of Decimal to Binary Conversion of Decimal to Binary Conversion of Decimal to Binary
Reduction of Powers Reduction of Powers Reduction of Powers
2210 = 2210 = 2210 =
(0x25)+(1x24)+(0x23)+(_x22)+(_x21)+(_x20 (0x25)+(1x24)+(0x23)+(1x22)+(_x21)+(_x20 (0x25)+(1x24)+(0x23)+(1x22)+(1x21)+(_x20
) ) 2 ) 1
2 =4 2 =2
23=8
22
-16
So the digit is 0 22
-16
4<6 So the digit is 1 22
-16
2=2 So the digit is 1
8>6
06 06 06
-04 -04
02 02
-02
00

Conversion of Decimal to Binary Conversion of Decimal to Binary Conversion of Decimal to Binary


Reduction of Powers Reduction of Powers Reduction of Powers
2210 = 2210 = 2210 =
(0x25)+(1x24)+(0x23)+(1x22)+(1x21)+(0x20 (0x25)+(1x24)+(0x23)+(1x22)+(1x21)+(0x20 (0x25)+(1x24)+(0x23)+(1x22)+(1x21)+(0x20
) ) )
22 0 left, so any remaining
-16
06 digits are zero
-04
2210 = 0101102 2210 = 0101102
02
-02
00 In unsigned arithmetic, leading
zeros are not significant
Conversion of Decimal to Binary Conversion of Decimal to Binary Concepts so far
Reduction of Powers Are leading
Reduction of Powers Yes!
2210 =
zeros significant
2210 =
❏ Addition
in signed We will see that
(0x25)+(1x24)+(0x23)+(1x22numbers?
)+(1x21)+(0x20 (0x2 )+(1x2 )+(0x2 )+(1x22next
5 4 3
time1)+(0x20
)+(1x2 ❏ Positional Number Systems
) )
❏ Conversion Among Bases
❏ Subtraction
2210 = 0101102 2210 = 0101102

In unsigned arithmetic, leading In unsigned arithmetic, leading


zeros are not significant zeros are not significant

Subtraction Subtraction Subtraction


How do computers subtract? How do computers subtract? How do computers subtract?
HLL Source HLL Source Machine
(e.g. Java, C++) (e.g. Java, C++) Code

C=A-B; C=A-B; SUB A,B,C

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

Subtraction Subtraction Decimal Subtraction


How do computers subtract? How do computers subtract?
HLL Source Machine Hardware HLL Source Machine Hardware 85
(e.g. Java, C++) Code (e.g. Java, C++) Code -07
Long way
C=A-B; SUB A,B,C C=A+(-B) C=A-B; SUB A,B,C C=A+(-B)
All the steps
For completeness we Some evil person may
will cover real ask you to subtract in
Addition Addition
Subtraction Base 7
Complement Complement
Arithmetic Arithmetic
Decimal Subtraction Decimal Subtraction Decimal Subtraction

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

Decimal Subtraction Decimal Subtraction Decimal Subtraction


We will borrow from
85 Since 5<7 we 85 the next column 85
-07 must take action -07 -07
to proceed

( 8x101) + ( 5x100) (8x101) + ( 5x100) ( 8x101) + ( 5x100)


-( 0x101) + ( 7x100) -(0x101) + ( 7x100) -( 0x101) + ( 7x100)

Each 101 equals 10x100

https://flickr.com/photos/67193564@N03/6173548853
Decimal Subtraction Decimal Subtraction Decimal Subtraction

85 85 85
-07 -07 -07

( 7x101) + (10x100) ( 7x101) + (10x100)


1 0
( 8x10 ) + ( 5x10 ) ( 8x101) + ( 5x100) ( 8x101) + ( 5x100)
-( 0x101) + ( 7x100) -( 0x101) + ( 7x100) -( 0x101) + ( 7x100)
( 8x100)

Decimal Subtraction Decimal Subtraction Binary Subtraction

85 85
-07 -07

( 7x101) + (10x100) ( 7x101) + (10x100)


( 8x101) + ( 5x100) ( 8x101) + ( 5x100)
-( 0x101) + ( 7x100) -( 0x101) + ( 7x100)
( 7x101) + ( 8x100) ( 7x101) + ( 8x100)

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

(1 x 22) + (0 x 21) + (0 x 20) (1 x 22) + (0 x 21) + (0 x 20)


- (0 x 22) + (0 x 21) + (1 x 20) - (0 x 22) + (0 x 21) + (1 x 20)

Binary Subtraction Binary Subtraction Binary Subtraction


1002 = 410 1002 = 410 1002 = 410
- 0012 = 110 - 0012 = 110 - 0012 = 110

Each 22 is the Each 22 is the


This is the first position same as two 21 same as two 21
that is not 0
(0 x 22) + (2 x 21) (0 x 22) +(10 x 21)
(1 x 22) + (0 x 21) + (0 x 20) (1 x 22) + (0 x 21) + (0 x 20) (1 x 22) + (0 x 21) + (0 x 20)
- (0 x 22) + (0 x 21) + (1 x 20) - (0 x 22) + (0 x 21) + (1 x 20) - (0 x 22) + (0 x 21) + (1 x 20)
Binary Subtraction Binary Subtraction Binary Subtraction
1002 = 410 1002 = 410 1002 = 410
- 0012 = 110 - 0012 = 110 1
Each 2 is the - 0012 = 110
2 same as two 20
Each 2 is the
same as two 21 + (1 x 20) + (1 x 20)
(1 x 21) (1 x 21) + (1 x 20) (1 x 21) + (1 x 20)
(0 x 22) + (1 x 21) (0 x 22) + (1 x 21) (0 x 22) + (1 x 21)
(1 x 22) + (0 x 21) + (0 x 20) (1 x 22) + (0 x 21) + (0 x 20) (1 x 22) + (0 x 21) + (0 x 20)
- (0 x 22) + (0 x 21) + (1 x 20) - (0 x 22) + (0 x 21) + (1 x 20) - (0 x 22) + (0 x 21) + (1 x 20)
(1 x 20)

Binary Subtraction Binary Subtraction Binary Subtraction


1002 = 410 1002 = 410 1002 = 410
- 0012 = 110 - 0012 = 110 - 0012 = 110
0112
+ (1 x 20) + (1 x 20) + (1 x 20)
(1 x 21) + (1 x 20) (1 x 21) + (1 x 20) (1 x 21) + (1 x 20)
(0 x 22) + (1 x 21) (0 x 22) + (1 x 21) (0 x 22) + (1 x 21)
(1 x 22) + (0 x 21) + (0 x 20) (1 x 22) + (0 x 21) + (0 x 20) (1 x 22) + (0 x 21) + (0 x 20)
- (0 x 22) + (0 x 21) + (1 x 20) - (0 x 22) + (0 x 21) + (1 x 20) - (0 x 22) + (0 x 21) + (1 x 20)
(1 x 21) + (1 x 20) (0 x 22) + (1 x 21) + (1 x 20) (0 x 22) + (1 x 21) + (1 x 20)
Binary Subtraction
1002 = 410 Can we speed up subtraction? Can we speed up subtraction?
- 0012 = 110
0112 = 310
+ (1 x 20) 100010
(1 x 21) + (1 x 20)
(0 x 22) + (1 x 21)
(1 x 22) + (0 x 21) + (0 x 20) Re-write the 1
- (0 x 22) + (0 x 21) + (1 x 20) followed by 0s
(0 x 22) + (1 x 21) + (1 x 20)

Can we speed up subtraction?


Can we speed up subtraction? Can we speed up subtraction?
1000
1 1 -0625
100010 = 0999 100010 = 0999

Re-write the 1 As 0 followed Re-write the 1 As 0 followed


followed by 0s by 9s plus 1 followed by 0s by 9s plus 1

How does this help?


Can we speed up subtraction? Can we speed up subtraction? Can we speed up subtraction?
1 1
1000 1000 = 0999 1000 = 0999
-0625 -0625 = -0625 -0625 = -0625

This requires multiple This requires multiple This requires multiple


This requires no
re-distributions of data re-distributions of data re-distributions of data
re-distributions

Can we speed up subtraction? Can we speed up subtraction? Can we speed up subtraction?


1 1 1
1000 = 0999 1000 = 0999 1000 = 0999
-0625 = -0625 -0625 = -0625 -0625 = -0625
5 75 375

This requires multiple This requires multiple This requires multiple


This requires no This requires no This requires no
re-distributions of data re-distributions of data re-distributions of data
re-distributions re-distributions re-distributions
Can we speed up subtraction? Can we speed up subtraction? Can we speed up subtraction?
1 1
1000 = 0999 1000 = 0999
-0625 = -0625 -0625 = -0625
0375 0375
2210 = 1 0 1 1 02
This requires multiple This requires multiple -1110 = -0 1 0 1 12
This requires no This requires no
re-distributions of data re-distributions of data
re-distributions re-distributions

Increases accuracy
(helps when working in different bases)

Can we speed up subtraction? Can we speed up subtraction? Can we speed up subtraction?

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

Can we speed up subtraction? Can we speed up subtraction? Can we speed up subtraction?

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

0 0 0 02 1 1 1 12 ❏ Positional Number Systems


❏ Conversion Among Bases
010 ❏ Subtraction
1510
❏ Hexadecimal
Break a binary number into Digits run 0 to (base - 1)
groups of 4 binary digits Digits 0 - 15 lead to a base of 16

Hexadecimal - base 16 Hexadecimal - base 16 Decimal Binary Hexadecimal


0000
0001
Name Base Digits (0 thru base-1) Name Base Digits (0 thru base-1) 0010
0011
Decimal 10 0-9 Decimal 10 0-9 0100
0101
Binary 2 0-1 Binary 2 0-1 0110
0111
Hexadecimal 16 0-(15) Hexadecimal 16 0-(15) 1000
(Hex) 1001
1010
1011
1100
Proper term would be senidenary we need something for digits above 9 1101
1110
1111
Decimal Binary Hexadecimal Decimal Binary Hexadecimal Decimal Binary Hexadecimal
00 0000 00 0000 0 00 0000 0
01 0001 01 0001 1 01 0001 1
02 0010 02 0010 2 02 0010 2
03 0011 03 0011 3 03 0011 3
04 0100 04 0100 4 04 0100 4
05 0101 05 0101 5 05 0101 5
06 0110 06 0110 6 06 0110 6
07 0111 07 0111 7 07 0111 7
08 1000 08 1000 8 08 1000 8
09 1001 09 1001 9 09 1001 9
10 1010 10 1010 10 1010 A
11 1011 11 1011 11 1011 B
12 1100 12 1100 12 1100 C
13 1101 13 1101 13 1101 D
14 1110 14 1110 14 1110 E
15 1111 15 1111 15 1111 F

Decimal Binary Hexadecimal


00 0000 0
Hex ↔ Binary 01 0001 1 Hex ↔ Binary
02 0010 2
03 0011 3
… lookup in table 04 0100 4 … lookup in table
05 0101 5
06 0110 6
07 0111 7
1 1 0 0 0 1 0 1 1 1 1 02 08 1000 8 1 1 0 0 0 1 0 1 1 1 1 02
09 1001 9
10 1010 A
11 B
C
1011
12 1100 C
13 1101 D
14 1110 E
15 1111 F
Decimal Binary Hexadecimal Decimal Binary Hexadecimal
00 0000 0 00 0000 0
01 0001 1 Hex ↔ Binary 01 0001 1
02 0010 2 02 0010 2
03 0011 3 03 0011 3
04 0100 4 … lookup in table 04 0100 4
05 0101 5 05 0101 5
06 0110 6 06 0110 6
07 0111 7 07 0111 7
08 1000 8 1 1 0 0 0 1 0 1 1 1 1 02 08 1000 8
09 1001 9 09 1001 9
10 1010 A 10 1010 A
11 B 11 B
C 5
1011 1011
12 1100 C 12 1100 C
13 1101 D 13 1101 D
14 1110 E 14 1110 E
15 1111 F 15 1111 F

Hex ↔ Binary Hex → Decimal Hex → Decimal


Expansion of Powers to get Decimal Expansion of Powers to get Decimal
… lookup in table
A 716 = ( Ax161) + ( 7x160) A 716 = ( Ax161) + ( 7x160)
1 1 0 0 0 1 0 1 1 1 1 02 A 716 = (10x161) + ( 7x160)

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

A 716 = ( Ax161) + ( 7x160) A 716 = ( Ax161) + ( 7x160) 16710=( x162) + ( x161) + (


A 716 = (10x161) + ( 7x160) A 716 = (10x161) + ( 7x160) x160)
A 716 = 160 + 7 A 716 = 160 + 7

Do math in decimal A 716 = 16710 Do math in decimal


when converting to when converting to
decimal decimal

Decimal → Hex Decimal → Hex Decimal → Hex


Reduction of Powers to get Hexadecimal Reduction of Powers to get Hexadecimal Reduction of Powers to get Hexadecimal

16710=( 0x162) + ( x161) + ( 16710=( 0x162) + ( Ax161) + ( 16710=( 0x162) + ( Ax161) + (


x160) x160
)
161=16 7x160)
162=256 So Digit is 0 10x16=160 160=1 Seven 1 in 7
Ten 16 in 167
256>167 11x16=176 7x1=7 So digit is “7”
So digit is “A”
160<167<176
We have 167-160=7 left
Addition in Hex Addition in Hex
Decimal → Hex
Reduction of Powers to get Hexadecimal
7 A16 7 A16
16710=( 0x162) + ( Ax161) + ( Work col by col
+ 5 E16 + 5 E16
7x160) - convert to decimal
- perform the operation
16710=A716
- convert result to hex

Addition in Hex Addition in Hex Addition in Hex

A16
7 A16 7 A16 7 A16
+ E16
+ 5 E16 + 5 E16 + 5 E16
Addition in Hex Addition in Hex Addition in Hex

A16 = 1010 A16 = 1010 A16 = 1010


7 A16 7 A16 7 A16
+ E16 = 1410 + E16 = 1410 + E16 = 1410
+ 5 E16 + 5 E16 + 5 E16
24 24 =

Greater than
the base (16)

Addition in Hex Addition in Hex Addition in Hex

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

Addition in Hex Addition in Hex Addition in Hex

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

Less than Less than


the base (16) the base (16)
Subtraction in Hex Subtraction in Hex Subtraction in Hex

816 = 810 816 = 810


D 816 D 816 Requires D 816
- E16 = 1410 - E16 = 1410 redistribution
- 5 E16 - 5 E16 - 5 E16

Subtraction in Hex Subtraction in Hex Subtraction in Hex

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

Subtraction in Hex Subtraction in Hex Subtraction in Hex

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.

Infinite Precision Arithmetic


Create Digits as needed Create Digits as needed Create Digits as needed

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

Finite Precision Arithmetic Finite Precision Arithmetic Examples using 4 bit


Digits are fixed by the Manufacturer
unsigned numbers
Addition Subtraction
Once you build hardware
Overflow
Can create a result that requires 1101 0001
more digits than are available + 0100 - 0100
the generation of a result
that does not fit in the data
size being used
Examples using 4 bit Examples using 4 bit Examples using 4 bit
unsigned numbers unsigned numbers unsigned numbers
Addition Subtraction Addition Subtraction Addition Subtraction

1101 0001 1101 0001 1101 0001


+ 0100 - 0100 + 0100 - 0100 + 0100 - 0100
13 + 4 13 + 4 13 + 4 1-4
Desired result = 17 Desired result = 17 Desired result = 17 Desired result = -3

17 > 15 which is the 17 > 15 which is the


largest value that largest 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

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)

Examples using 4 bit Examples using 4 bit The application


unsigned numbers unsigned numbers
Missile Control
Addition Subtraction Addition Subtraction
1 1 Missile is launched.
11 011 11 011 Reaches height A (20,000 ft)
1101 10001 1101 10001 Software says go to 2A
+ 0100 - 0100 + 0100 - 0100 2*A Missile turns down
0001 1101 0001 1101 ...
Overflow detected Result
by borrow into the presented A
Most Significant
Digit (MSD)

You might also like