Presentation is loading. Please wait.

Presentation is loading. Please wait.

Number Representations and Computer Arithmetic. CS 21a 9/23/02 Odds and Ends Slide 2 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University.

Similar presentations


Presentation on theme: "Number Representations and Computer Arithmetic. CS 21a 9/23/02 Odds and Ends Slide 2 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University."— Presentation transcript:

1 Number Representations and Computer Arithmetic

2 CS 21a 9/23/02 Odds and Ends Slide 2 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University “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

3 CS 21a 9/23/02 Odds and Ends Slide 3 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University 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

4 CS 21a 9/23/02 Odds and Ends Slide 4 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University 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

5 CS 21a 9/23/02 Odds and Ends Slide 5 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University 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 10 0 d0d0 9 10 1 d1d1 7 10 2 d2d2 3 10 3 d3d3 0 10 4 d4d4 0 10 5 d5d5 0 10 6 d6d6 0 10 7 d7d7 3792 10 = 3  10 3 + 7  10 2 + 9  10 1 + 2  10 0 = 3000 + 700 + 90 + 2 3792 + 531 ??? Range 0 to 10 n - 1 1 “carry 1” because 9+3 = 12 0 4 3 2 3

6 CS 21a 9/23/02 Odds and Ends Slide 6 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Binary Numbers (Unsigned) Binary Integer Representation “base” of place values is 2, not 10 0 2020 b0b0 0 2121 b1b1 1 2 b2b2 0 2323 b3b3 0 2424 b4b4 1 2525 b5b5 1 2626 b6b6 0 2727 b7b7 01100100 2 = 2 6 + 2 5 + 2 2 = 64 + 32 + 4 = 100 10 Range 0 to 2 n - 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”

7 CS 21a 9/23/02 Odds and Ends Slide 7 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Converting from Binary to Decimal 2 0 = 1 2 1 = 2 2 2 = 4 2 3 = 8 2 4 = 16 2 5 = 32 2 6 = 64 2 7 = 128 2 8 = 256 2 9 = 512 2 10 = 1,024 or “1K” VERY USEFUL trick for a CS/MIS person … Memorize powers of 2 from 2 0 up to 2 10 Lets you approximate any power of 2 “1 KB” is actually 1,024 bytes, not 1000 bytes “1 MB” is 1K*1KB = 2 20 bytes = 1,048,576 bytes = approximately 1 million bytes “1 GB” is 1K * 1MB = 2 30 bytes = (approx 1 billion) Example 1: what is 2 16 ? 2 6 * 2 10 = 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 = 2 n -1 = 2 32 - 1 2 32 = 2 2 * 2 30 = 4 * 1 G = approximately 4 billion = actually 4*1024*1024*1024 = 4,294,967,296 (minus 1)

8 CS 21a 9/23/02 Odds and Ends Slide 8 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University 2 0 = 1 2 1 = 2 2 2 = 4 2 3 = 8 2 4 = 16 2 5 = 32 2 6 = 64 2 7 = 128 2 8 = 256 2 9 = 512 2 10 = 1,024 or “1K” More practice 0b00000010 0b00110001 0b00001110 0b10110001 Converting from Binary to Decimal

9 CS 21a 9/23/02 Odds and Ends Slide 9 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University From Decimal to Binary General rule: Divide and write remainder from 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 0 1896 / 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 = 2 11 +2 10 +2 9 +2 7 +2 6 +2 4 = 3792

10 CS 21a 9/23/02 Odds and Ends Slide 10 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University 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” because 9+3 = 12 0 4 3 2 3 1 1 1 “carry 1” because 1+1 = 10 1 0 1 0 1

11 CS 21a 9/23/02 Odds and Ends Slide 11 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Binary Arithmetic A bits 1 1 1 0 B 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 bits1 1 1 0 0 sum bits 0 1 0 1 carry-out bits1 1 1 1 0

12 CS 21a 9/23/02 Odds and Ends Slide 12 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University 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

13 CS 21a 9/23/02 Odds and Ends Slide 13 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University 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 2 n-1 ) 2 10 0 010 -2 10 1 010 0 10 0 000 -0 10 1 000 2 10 0 010 -2 10 1 101 0 10 0 000 -0 10 1 111 2 10 0 010 -2 10 1 110 0 10 0 000 -0 10 0 000

14 CS 21a 9/23/02 Odds and Ends Slide 14 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University 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 01010011 1100 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 0101 1010 flip -2 0 0 1 0 2

15 CS 21a 9/23/02 Odds and Ends Slide 15 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University 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 01010011 1100 1101 flip +1 -3 in 2’s complement form 2 ignore overflow 0 0 1 1 +1 0 1 1 1 1 1 0 3 - 5 = -2 0011 -5 in 2’s complement form 0101 1010 1011 flip +1 -2 0001 0010 flip +1 (flip+1 also gives positive of negative number) 2

16 CS 21a 9/23/02 Odds and Ends Slide 16 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University 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): ?

17 CS 21a 9/23/02 Odds and Ends Slide 17 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University 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 2020 b0b0 0 2121 b1b1 1 2 b2b2 0 2323 b3b3 0 2424 b4b4 1 2525 b5b5 1 2626 b6b6 0 2727 b7b7 64 16 = 6*16 10 + 4 = 100 10 aka “0x64”

18 CS 21a 9/23/02 Odds and Ends Slide 18 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University 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?


Download ppt "Number Representations and Computer Arithmetic. CS 21a 9/23/02 Odds and Ends Slide 2 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University."

Similar presentations


Ads by Google