skills outcomes
play

Skills & Outcomes You should know and be able to apply the - PowerPoint PPT Presentation

2.1 2.2 Skills & Outcomes You should know and be able to apply the following skills with confidence Unit 2 Perform addition & subtraction in unsigned & 2's complement system Integer Operations Determine if overflow has


  1. 2.1 2.2 Skills & Outcomes • You should know and be able to apply the following skills with confidence Unit 2 – Perform addition & subtraction in unsigned & 2's complement system Integer Operations – Determine if overflow has occurred (Arithmetic, Overflow, Bitwise Logic, Shifting) – Perform bitwise operations on numbers – Perform logic and arithmetic shifts and understand how they can be used for multiplication/division – Understand arithmetic in binary and hex 2.3 2.4 Binary Arithmetic • Can perform all arithmetic operations (+,-,*, ÷ ) on binary numbers • Can use same methods as in decimal – Still use carries and borrows, etc. – Only now we carry when sum is __ or more rather than 10 or more (decimal) – We borrow ___’s not 10’s from other columns UNSIGNED BINARY ARITHMETIC • Easiest method is to add bits in your head in decimal (1+1 = 2) then convert the answer to binary (2 10 = 10 2 )

  2. 2.5 2.6 Binary Addition Binary Addition & Subtraction • In decimal addition we carry when the sum is 10 or more • In binary addition we carry when the sum is 2 or more 0 1 1 1 (7) 1 0 1 0 (10) • Add bits in binary to produce a sum bit and a carry bit + 0 0 1 1 (3) - 0 1 0 1 (5) 1 0 1 1 0 + 1 + 0 + 1 + 0 01 01 10 00 no need sum bit no need sum bit carry 1 sum bit no need sum bit to carry to carry into next to carry column of bits 2.7 2.8 Binary Addition Binary Addition 1 2 0 10 0 0 0110 (6) 0110 (6) 1 + 1 + 0111 (7) + 0111 (7) + 1 110 01 1101 (13) 0110 (6) 1101 (13) 10 8 4 2 1 + 0111 (7) carry bit sum bit carry bit sum bit 1101 (13) 3 110 1 4 110 1 0110 (6) 0 0110 (6) 1 + 0111 (7) + 0 + 0111 (7) + 1 1101 (13) 01 1101 (13) 11 carry bit sum bit carry bit sum bit

  3. 2.9 2.10 Hexadecimal Arithmetic Binary Multiplication • Like decimal multiplication, find each partial product • Same style of operations and _________ them, then sum them up – Carry when sum is 16 or more, etc. • Multiplying two n- bit numbers yields at most a ______ -bit product 0 1 1 0 (6) 4 D 16 * 0 1 0 1 (5) 16 1 + B 5 16 Partial Products 16 1 + Sum of the partial products 2.11 2.12 Binary Division • Use the same long division techniques as in decimal • Dividing two n- bit numbers may yield an n-bit quotient and n-bit remainder 0 1 0 1 r.1 (5 r.1) 10 "Taking the 2's complement" (2) 10 10 1 0 1 1 (11) 10 SUBTRACTION THE EASY WAY -1 0 0 1 -0 0 1 1 -1 0 0 1

  4. 2.13 2.14 Modulo Arithmetic Taking the Negative CS:APP 2.3.3 • The primary difference between how humans • Question : Given a number in 2’s complement how and computers perform arithmetic is the finite do we find its negative (i.e. -1 * X) _______________ of computers • Answer : By "__________________________" – As humans we can use more digits (precision) as needed – 0110 = +6 => -6 = 1010 – Computers can only used a _________ set of bits – Operation defined as: • Much like the odometer on your car once you go too many miles the values will wrap from 999999 to 000000 1. ______________________________ • Essentially all computer arithmetic is ___________ 2. ______________________________ arithmetic (i.e. finish with the same # of bits as we start with) • If we have a width of w bits, then all operations are module ______ – See next slides for example • This leads to alternate approaches to arithmetic – Example: Consider how you could change the clock time from 5 p.m. to 3 p.m. if you can't ________ hours 2.15 2.16 Taking the 2’s Complement Taking the 2’s Complement • Invert (flip) each bit 1 2 -32 16 8 4 2 1 Original # = 0 0000 Original number = -22 101010 -32 16 8 4 2 1 (take the 1’s Original number = +19 010011 Take the complement) Take the 2’s complement 2’s complement yields the negative of a number – 1’s become 0’s Bit flip is called the 1’s 2’s comp. of 0 is __ complement of a number Resulting number = +22 – 0’s become 1’s 3 1000 Resulting number = -19 Taking the 2’s complement Original # = -8 • Add 1 (drop final again yields the original number (the operation is carry-out, if any) Take the symmetric) 2’s complement Back to original = -22 Negative of -8 is ___ (i.e. no positive Important: Taking the 2’s complement is equivalent to equivalent, but this is taking the negative (negating) not a huge problem)

  5. 2.17 2.18 Radix Complement 12 12 . 11 1 11 1 10 2 10 2 Clock Analogy 9 3 9 3 4-2 = 4+10 8 4 8 4 5 7 7 5 6 6 00 01 00 01 99 99 98 02 98 02 10’s complement 03 03 04-02 = 04 + 98 . . The same algorithms regardless of unsigned or signed 04 04 . . . . . . ADDITION AND SUBTRACTION 0000 0001 0000 0001 1111 1111 1110 0010 1110 0010 2’s complement 0011 0011 . . 0100 - 0010 = 0100 + 1110 0100 0100 . . . . . . When using modulo arithmetic , subtraction can always be converted to addition. 2.19 2.20 2’s Complement Addition 2’s Complement Addition/Subtraction CS:APP 2.3.1 CS:APP 2.3.2 • Addition • No matter the sign of the operands just add as normal – Sign of the numbers _______________ • Drop any extra carry out – Add column by column 0011 (3) 1101 (-3) – Drop any final ______________ • The secret to modulo arithmetic + 0010 (2) + 0010 (2) • Subtraction – Any subtraction (A-B) can be converted to addition (_______) by taking the ______________of B – (A-B) becomes (A + _________ ) 0011 (3) 1101 (-3) – Drop any carry-out + 1110 (-2) + 1110 (-2) • The secret to modulo arithmetic

  6. 2.21 2.22 Unsigned and Signed Addition 2’s Complement Subtraction • Take the 2’s complement of the subtrahend (bottom #) • Addition process is the _________ for both and add to the original minuend (top #) • Drop any extra carry out unsigned and signed numbers – Add columns right to left 0011 (+3) 1101 (-3) • Examples: - 0010 (+2) - 1110 (-2) If unsigned If signed 1001 + 0011 2.23 2.24 Important Note Unsigned and Signed Subtraction • Subtraction process is the same for both • Almost all computers use 2's complement unsigned and signed numbers because… – Convert A – B to A + Comp. of B • The same addition and subtraction – Drop any final carry out ___________ can be used on unsigned and 2's • Examples: complement (signed) numbers • Thus we only need one set of If unsigned If signed 1100 (12) (-4) _____________________________ to - 0010 (2) (2) perform operations on both unsigned and signed numbers If unsigned If signed

  7. 2.25 2.26 Overflow CS:APP 2.3.1 CS:APP 2.3.2 • Overflow occurs when the result of an arithmetic operation is _____________ ________________________________ • Conditions and tests to determine overflow depend on the ________ being OVERFLOW used – Different algorithms for detecting overflow based on _____________________ 2.27 2.28 Unsigned Overflow 2’s Complement Overflow 0 Overflow occurs when you cross -1 +1 0000 this discontinuity 0 1111 0001 -2 +15 +2 +1 0000 1110 0010 1111 0001 +14 -3 +2 +3 1101 1110 0010 0011 5 + 7 = +12 +13 +3 1101 0011 -4 1100 0100 +4 Plus 7 -6 + -4 = -10 10 + 7 = 17 +12 1100 0100 +4 1011 0101 -5 +5 With 4-bit 2’s complement 1010 With 4-bit unsigned numbers we 10 0110 1011 0101 +11 numbers we can only represent +5 -6 1001 0111 +6 can only represent 0 – 15. Thus, 1010 -8 to +7. Thus, we say overflow 0110 1000 we say overflow has occurred. +7 -7 has occurred. +10 1001 0111 +6 -8 1000 +7 +9 +8 Overflow occurs when you cross this discontinuity

  8. 2.29 2.30 Overflow in Addition Overflow in Subtraction • Overflow occurs when the result of the addition • Overflow occurs when the result of the subtraction cannot be represented with the given number of cannot be represented with the given number bits. of bits. • Tests for overflow: • Tests for overflow: – Unsigned: if _______ [expect negative result] – Unsigned: if _________ [result __________ than inputs] – Signed: ________________[result has inappropriate sign] – Signed: if _____________ [result has inappropriate sign] If unsigned If signed 0111_ 0111 (7) (7) 0111 A 1 1 If unsigned If signed 0 1 If unsigned If signed - 1000 (8) (-8) 0111 1’s comp. of B 1101 (13) (-3) 0110 (6) (6) (-1) (15) + 1 Add 1 + 0100 (4) (4) + 0101 (5) (5) 1111 (15) (-1) Desired 0001 (17) (+1) 1011 (11) (-5) Results If unsigned If signed Overflow No Overflow No Overflow Overflow Overflow Overflow Cout = 1 n + p Cout = 0 p + p = n Cout = 0 p + p = n 2.31 2.32 Binary Multiplication CS:APP 2.3.4 • Multiplying two n- bit numbers yields at most a 2*n -bit product • Multiplication operations on a modern processor can take ______ times longer than addition operations 0 1 1 0 (6) * 0 1 0 1 (5) MULTIPLICATION AND DIVISION 0 1 1 0 0 0 0 0 Partial Products 0 1 1 0 + 0 0 0 0 0 0 1 1 1 1 0 Sum of the partial products

Recommend


More recommend