numbers and arithmetic
play

Numbers and Arithmetic Prof. Hakim Weatherspoon CS 3410 Computer - PowerPoint PPT Presentation

Numbers and Arithmetic Prof. Hakim Weatherspoon CS 3410 Computer Science Cornell University [Weatherspoon, Bala, Bracy, and Sirer] Big Picture: Building Processor Simplified Single-cycle processor 2 Goals for Today Binary Operations


  1. Numbers and Arithmetic Prof. Hakim Weatherspoon CS 3410 Computer Science Cornell University [Weatherspoon, Bala, Bracy, and Sirer]

  2. Big Picture: Building Processor Simplified Single-cycle processor 2

  3. Goals for Today Binary Operations • Number representations • One-bit and four-bit adders • Negative numbers and two’s compliment • Addition (two’s compliment) • Detecting and handling overflow • Subtraction (two’s compliment) 3

  4. Number Representations Recall: Binary • Two symbols (base 2): true and false; 1 and 0 • Basis of Logic Circuits and all digital computers So, how do we represent numbers in Binary (base 2)? 4

  5. Number Representations Recall: Binary • Two symbols (base 2): true and false; 1 and 0 • Basis of Logic Circuits and all digital computers So, how do we represent numbers in Binary (base 2)? • We can represent numbers in Decimal (base 10). - E.g. 6 3 7 10 2 10 1 10 0 • Can just as easily use other bases 1 0 0 1 1 1 1 1 0 1 - Base 2 — Binary 2 9 2 8 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 0o 1 1 7 5 - Base 8 — Octal 8 3 8 2 8 1 8 0 - Base 16 — Hexadecimal 0x 2 7 d 16 2 16 1 16 0 5

  6. Number Representations Recall: Binary • Two symbols (base 2): true and false; 1 and 0 • Basis of Logic Circuits and all digital computers So, how do we represent numbers in Binary (base 2)? • We can represent numbers in Decimal (base 10). - E.g. 6 3 7 6∙10 2 + 3∙10 1 + 7∙10 0 = 637 10 2 10 1 10 0 • Can just as easily use other bases 1∙2 9 +1∙2 6 +1∙2 5 +1∙2 4 +1∙2 3 +1∙2 2 +1∙2 0 = 637 - Base 2 — Binary 1∙8 3 + 1∙8 2 + 7∙8 1 + 5∙8 0 = 637 - Base 8 — Octal 2∙16 2 + 7∙16 1 + d∙16 0 = 637 - Base 16 — Hexadecimal 2∙16 2 + 7∙16 1 + 13∙16 0 = 637 6

  7. Number Representations: Activity #1 Counting How do we count in different bases? • Dec (base 10) Bin (base 2) Oct (base 8) Hex (base 16) 0 0 0 0 1 1 1 1 2 10 2 2 3 11 3 3 4 100 4 4 0b 1111 1111 = ? 5 101 5 5 0b 1 0000 0000 = ? 6 110 6 6 7 111 7 7 0o 77 = ? 8 1000 10 8 9 1001 11 9 0o 100 = ? 10 1010 12 a 11 1011 13 b 0x ff = ? 12 1100 14 c 13 1101 15 d 0x 100 = ? 14 1110 16 e 15 1111 17 f 16 1 0000 20 10 17 1 0001 21 11 18 1 0010 22 12 . . . . . . . . 99 100 7

  8. Number Representations: Activity #1 Counting How do we count in different bases? • Dec (base 10) Bin (base 2) Oct (base 8) Hex (base 16) 0 0 0 0 1 1 1 1 2 10 2 2 3 11 3 3 4 100 4 4 0b 1111 1111 = 255 5 101 5 5 0b 1 0000 0000 = 256 6 110 6 6 7 111 7 7 0o 77 = 63 8 1000 10 8 9 1001 11 9 0o 100 = 64 10 1010 12 a 11 1011 13 b 0x ff = 255 12 1100 14 c 13 1101 15 d 0x 100 = 256 14 1110 16 e 15 1111 17 f 16 1 0000 20 10 17 1 0001 21 11 18 1 0010 22 12 . . . . . . . . 99 100 8

  9. Number Representations How to convert a number between different bases? Base conversion via repetitive division • Divide by base, write remainder, move left with quotient • 637  8 = 79 remainder 5 lsb (least significant bit) 79  8 = 9 remainder 7 • 9  8 = 1 remainder 1 • 1  8 = 0 remainder 1 • msb (most significant bit) • 637 = 0o 1175 msb lsb 9

  10. MP1 Number Representations Convert a base 10 number to a base 2 number Base conversion via repetitive division • Divide by base, write remainder, move left with quotient • 637  2 = 318 remainder 1 lsb (least significant bit) • 318  2 = 159 remainder 0 • 159  2 = 79 remainder 1 79  2 = 39 remainder 1 • 39  2 = 19 remainder 1 • 19  2 = 9 remainder 1 • 9  2 = 4 remainder 1 • 4  2 = 2 remainder 0 • 2  2 = 1 remainder 0 • 1  2 = 0 remainder 1 • msb (most significant bit) 637 = 10 0111 1101 (can also be written as 0b10 0111 1101) 10 msb lsb

  11. Slide 10 MP1 Meghna Pancholi, 12/5/2018

  12. Clicker Question! Convert the number 657 10 to base 16 What is the least significant digit of this number? a) D b) F c) 0 d) 1 e) 11 11

  13. Clicker Question! Convert the number 657 10 to base 16 What is the least significant digit of this number? a) D b) F c) 0 d) 1 e) 11 12

  14. Number Representations Convert a base 10 number to a base 16 number Base conversion via repetitive division • Divide by base, write remainder, move left with quotient lsb • 657  16 = 41 remainder 1 41  16 = 2 remainder 9 • 2  16 = 0 remainder 2 • msb Thus, 657 = 0x291 13

  15. Number Representations Convert a base 10 number to a base 16 number Base conversion via repetitive division • Divide by base, write remainder, move left with quotient lsb dec = hex = bin • 637  16 = 39 remainder 13 10 = 0xa = 1010 39  16 = 2 remainder 7 11 = 0xb = 1011 • 12 = 0xc = 1100 2  16 = 0 remainder 2 • 13 = 0xd = 1101 msb 14 = 0xe = 1110 15 = 0xf = 1111 637 = 0x 2 7 13 = ? Thus, 637 = 0x27d 14

  16. Number Representations Convert a base 2 number to base 8 (oct) or 16 (hex) Binary to Hexadecimal • Convert each nibble (group of four bits) from binary to hex • A nibble (four bits) ranges in value from 0…15, which is one hex digit - Range: 0000…1111 (binary) => 0x0 …0xF (hex) => 0…15 (decimal) • E.g. 0b 10 0111 1101 2 7 d  0x27d - Thus, 637 = 0x27d = 0b10 0111 1101 Binary to Octal • Convert each group of three bits from binary to oct • Three bits range in value from 0…7, which is one octal digit - Range: 0000…1111 (binary) => 0x0 …0xF (hex) => 0…15 (decimal) • E.g. 0b1 001 111 101 1 1 7 5  0o 1175 - Thus, 637 = 0o1175 = 0b10 0111 1101 15

  17. Number Representations Summary We can represent any number in any base • Base 10 – Decimal 6 3 7 6∙10 2 + 3∙10 1 + 7∙10 0 = 637 10 2 10 1 10 0 • Base 2 — Binary 1∙2 9 +1∙2 6 +1∙2 5 +1∙2 4 +1∙2 3 +1∙2 2 1 0 0 1 1 1 1 1 0 1 +1∙2 0 = 637 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 2 9 2 8 • Base 8 — Octal 1∙8 3 + 1∙8 2 + 7∙8 1 + 5∙8 0 = 637 0o 1 1 7 5 8 3 8 2 8 1 8 0 • Base 16 — Hexadecimal 2∙16 2 + 7∙16 1 + d∙16 0 = 637 0x 2 7 d 2∙16 2 + 7∙16 1 + 13∙16 0 = 637 16 16 2 16 1 16 0

  18. Achievement Unlocked! There are 10 types of people in the world: Those who understand binary And those who do not And those who know this joke was written in base 2 17

  19. Takeaway Digital computers are implemented via logic circuits and thus represent all numbers in binary (base 2). We (humans) often write numbers as decimal and hexadecimal for convenience, so need to be able to convert to binary and back (to understand what the computer is doing!). 18

  20. Today’s Lecture Binary Operations • Number representations • One-bit and four-bit adders • Negative numbers and two’s compliment • Addition (two’s compliment) • Detecting and handling overflow • Subtraction (two’s compliment) 19

  21. Next Goal Binary Arithmetic: Add and Subtract two binary numbers 20

  22. Binary Addition How do we do arithmetic in binary? 1 • Addition works the same way 183 regardless of base + 254 • Add the digits in each position 437 • Propagate the carry Carry-in Carry-out Unsigned binary addition is pretty 1 1 1 001110 easy • Combine two bits at a time + 011100 • Along with a carry 1 1 0 1 0 0 21

  23. Binary Addition How do we do arithmetic in binary? 1 • Addition works the same way 183 regardless of base + 254 • Add the digits in each position 437 • Propagate the carry 111 Unsigned binary addition is pretty 001110 easy • Combine two bits at a time + 011100 • Along with a carry 101010 22

  24. Binary Addition • Binary addition requires • Add of two bits PLUS carry-in • Also, carry-out if necessary 23

  25. 1-bit Adder Half Adder A B • Adds two 1-bit numbers • Computes 1-bit result and 1- C out bit carry • No carry-in S Clicker Question A B C out S What is the equation for C out ? a) A + B 0 0 b) AB 0 1 A  B c) 1 0 d) A + !B 1 1 e) !A!B 24

  26. 1-bit Adder Half Adder A B • Adds two 1-bit numbers • Computes 1-bit result and 1- C out bit carry • No carry-in S Clicker Question A B C out S What is the equation for C out ? a) A + B 0 0 b) AB 0 1 A  B c) 1 0 d) A + !B 1 1 e) !A!B 25

  27. 1-bit Adder Half Adder A B • Adds two 1-bit numbers • Computes 1-bit result and 1- C out bit carry • No carry-in S � B + A B � • S = A • C out = AB A B C out S 0 0 0 0 A B C out 0 1 0 1 1 0 0 1 1 1 1 0 S 26

  28. 1-bit Adder Half Adder A B • Adds two 1-bit numbers • Computes 1-bit result and 1- C out bit carry • No carry-in S � B + A B � = A  B • S = A • C out = AB A B C out S 0 0 0 0 A B C out 0 1 0 1 1 0 0 1 1 1 1 0 S 27

  29. 1-bit Adder with Carry Full Adder A B • Adds three 1-bit numbers • Computes 1-bit result and 1- C out C in bit carry • Can be cascaded S Now You Try: A B C in C out S 1. Fill in Truth Table 0 0 0 2. Create Sum-of-Product Form 0 0 1 0 1 0 3. Minimization the equation 0 1 1 1. Karnaugh Maps ( coming 1 0 0 soon! ) 1 0 1 2. Algebraic minimization 1 1 0 4. Draw the Logic Circuits 1 1 1 28

Recommend


More recommend