Upload
marcia-fletcher
View
215
Download
0
Embed Size (px)
Citation preview
Number Representations andComputer Arithmetic
Odds and EndsSlide 2
© Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University
CS 21a9/23/02
“Computing” with Computers
Back in the “old days” (WW II time), the word “computer” meant this
• “Computers” were used to compute things such as trajectory tables, etc.– redundant
“computers” for reliability and speed• Quite effective!
– new computers were tested against human computers
Odds and EndsSlide 3
© Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University
CS 21a9/23/02
Of course, we don’t do things that way anymore!
But HOW do (electronic) computers compute?
The trick is to use BINARY numbers use 1’s and 0’s corresponds on
current/voltage being ON or OFF
easy to implement resilient against noise
“Computing” with Computers
Odds and EndsSlide 4
© Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University
CS 21a9/23/02
Java types: int and double
int range:-2,147,483,648 to 2,147,483,647
double range: 4.94e-324 to 1.80e+308
These ranges depend on: Storage size Internal data representation
Odds and EndsSlide 5
© Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University
CS 21a9/23/02
Review: Decimal Numbers Integer Representation
number is sum of DIGIT * “place value”
3 7 9 2 + 0 5 3 1
Adding two decimal numbers add by “place value”, one digit at a time
2
100
d0
9
101
d1
7
102
d2
3
103
d3
0
104
d4
0
105
d5
0
106
d6
0
107
d7
379210= 3 103 + 7 102 + 9 101 + 2 100
= 3000 + 700 + 90 + 2
3792 + 531
???
Range0 to 10n - 1
1 “carry 1” because9+3 = 12
0 4 3 2 3
Odds and EndsSlide 6
© Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University
CS 21a9/23/02
Binary Numbers
(Unsigned) Binary Integer Representation “base” of place values is 2, not 10
0
20
b0
0
21
b1
1
22
b2
0
23
b3
0
24
b4
1
25
b5
1
26
b6
0
27
b7
011001002 = 26 + 25 + 22
= 64 + 32 + 4 = 10010
Range0 to 2n - 1
• Humans can naturally count up to 10 values,
• But computers can count only up to 2 values (OFF and ON, or 0 and 1)
aka “0b01100100”
Odds and EndsSlide 7
© Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University
CS 21a9/23/02
Converting from Binary to Decimal
20 = 121 = 222 = 423 = 824 = 1625 = 3226 = 6427 = 12828 = 25629 = 512210 = 1,024
or “1K”
VERY USEFUL trick for a CS/MIS person … Memorize powers of 2 from 20 up to 210
Lets you approximate any power of 2 “1 KB” is actually 1,024 bytes, not 1000 bytes “1 MB” is 1K*1KB = 220 bytes
= 1,048,576 bytes = approximately 1 million bytes
“1 GB” is 1K * 1MB = 230 bytes = (approx 1 billion) Example 1: what is 216?
26 * 210 = 64 * 1024 = 64 K = 65,536 Example 2:
The Pentium processor does integer math with 32-bit numbers. What’s the highest unsigned number it can handle (approximately)?
range = 2n-1 = 232 - 1232 = 22 * 230
= 4 * 1 G = approximately 4 billion = actually 4*1024*1024*1024 = 4,294,967,296 (minus 1)
Odds and EndsSlide 8
© Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University
CS 21a9/23/02
20 = 121 = 222 = 423 = 824 = 1625 = 3226 = 6427 = 12828 = 25629 = 512210 = 1,024
or “1K”
More practice
0b00000010
0b00110001
0b00001110
0b10110001
Converting from Binary to Decimal
Odds and EndsSlide 9
© Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University
CS 21a9/23/02
From Decimal to Binary
General rule: Divide and write remainderfrom right to left
Why this works remainder gives bit 0 odd numbers have a 1 in bit 0
Note that this rule works in converting decimal to any base
e.g., HEX numbers (more later)
3792 / 2 = 1896 rem 01896 / 2 = 948 rem 0 948 / 2 = 474 rem 0 474 / 2 = 237 rem 0 237 / 2 = 118 rem 1 118 / 2 = 59 rem 0 59 / 2 = 29 rem 1 29 / 2 = 14 rem 1 14 / 2 = 7 rem 0 7 / 2 = 3 rem 1 3 / 2 = 1 rem 1 1 / 2 = 0 rem 1
0b111011010000= 211+210+29+27+26+24
= 3792
Odds and EndsSlide 10
© Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University
CS 21a9/23/02
Binary Arithmetic
1 1 1 0 + 0 1 1 1
Adding two binary numbers same, but “1 + 1 = 10”
14 + 7
21
3 7 9 2 + 0 5 3 1
Adding two decimal numbers
3792 + 531
???
1 “carry 1” because9+3 = 12
0 4 3 2 3
1 1 1 “carry 1” because1+1 = 10
1 0 1 0 1
Odds and EndsSlide 11
© Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University
CS 21a9/23/02
Binary Arithmetic
A bits 1 1 1 0B bits + 0 1 1 1
In general: Add up to 3 bits at a
time per place value A and B “carry in”
Output 2 bits at a time sum bit for that place
value “carry out” bit
(becomes carry-in of next bit)
carry-in bits 1 1 1 0 0
sum bits 0 1 0 1carry-out bits 1 1 1 1 0
Odds and EndsSlide 12
© Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University
CS 21a9/23/02
So what about negative values?
In math, it is easy to represent negative values
Just put a negative sign (-) prefix In computers, we extend the binary notation
in order to support signed values We can use the following 3 methods:
Sign and magnitude (using the Most Significant Bit or MSB)
1’s complement 2’s complement
Odds and EndsSlide 13
© Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University
CS 21a9/23/02
Signed Integers Sign-and-Magnitude
MSB represents sign bit (0 for positive, 1 for negative)
has 2 “zeroes” 1’s complement
flip bits easy to do subtraction STILL has 2 “zeros”
2’s complement flip bits, then add 1 easy subtraction
(just negate, then add) has only 1 zero Another interpretation:
add place values as before, except that MSB is negative(i.e., MSB is place value is 2n-1)
210 0 010
-210 1 010
010 0 000
-010 1 000
210 0 010
-210 1 101
010 0 000
-010 1 111
210 0 010
-210 1 110
010 0 000
-010 0 000
Odds and EndsSlide 14
© Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University
CS 21a9/23/02
Binary Subtraction 1’s complement subtraction: flip the negative
0 1 0 1 +1 1 0 0
1 0 0 0 1
5 - 3 = 2
0101 00111100
flip
-3 in 1’s complement form
1 Add the carry overflow
0 0 1 1 +1 0 1 0
1 1 0 1
3 - 5 = -2
0011
-5 in 1’s complement form
01011010
flip
-2
0 0 1 0
2
Odds and EndsSlide 15
© Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University
CS 21a9/23/02
Binary Subtraction 2’s complement subtraction: flip then add 1
0 1 0 1 +1 1 0 1
1 0 0 1 0
5 - 3 = 2
0101 001111001101
flip+1
-3 in 2’s complement form
2ignoreoverflow
0 0 1 1 +1 0 1 1
1 1 1 0
3 - 5 = -2
0011
-5 in 2’s complement form
010110101011
flip+1
-200010010
flip+1
(flip+1 also gives positive of negative number)
2
Odds and EndsSlide 16
© Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University
CS 21a9/23/02
Range of binary numbers
Java (and most computer platforms today) use 2’s comp.
Hence the range for the different int types
byte (8 bits): ? short (16 bits): ? int (32 bits): -2,147,483,648 to
2,147,483,647 long (64 bits): ?
Odds and EndsSlide 17
© Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University
CS 21a9/23/02
Hexadecimal (Hex) Numbers Base 16 Each hex digit goes
from 0-9, then A-F Hex is a convenient
shortform for binary 4 bits = 1 hex digit
(aka nibble) 1 byte = 8 bits = 2 hex digits Another useful trick:
memorize binary of 0 to F Addition and conversion
to/from decimal is similar except use base 16 instead of 2
0
20
b0
0
21
b1
1
22
b2
0
23
b3
0
24
b4
1
25
b5
1
26
b6
0
27
b7
6416 = 6*1610 + 4 = 10010
aka “0x64”
Odds and EndsSlide 18
© Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University
CS 21a9/23/02
Some Exercises
What’s the range of an n-bit sign-mag integer? How about 1’s comp? 2’s comp?
Convert the ff signed 8-bit values to decimal 0b01010101, 0b1110111, 0x14, 0x41
Convert the ff to 16-bit binary and hex numbers: 413, 39, 1045, -3, -124, -134
Add the following pairs 0b00001011 + 0b00100100, 0x3F + 0x2F
If 0x7F and 0x32 are signed 8-bit integers, what’s wrong with adding them and storing the result in a byte?
Puzzle: How can I use my 10 fingers to count up to 1000?