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.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 0 1 1 + 0 + 1 + 0 + 1 00 01 01 10 no need sum bit no need sum bit no need sum bit carry 1 sum bit to carry to carry to carry into next column of bits 2.7 2.8 Binary Addition Binary Addition 1 2 0 10 0 0 011 0 (6) 01 10 (6) 1 + 1 110 + 011 1 (7) + 01 11 (7) + 1 01 0110 (6) 110 1 (13) 11 01 (13) 10 8 4 2 1 + 0111 (7) carry bit sum bit carry bit sum bit 1101 (13) 3 110 1 110 1 4 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 2.9 2.10 Hexadecimal Arithmetic Binary Multiplication • Same style of operations • Like decimal multiplication, find each partial product – Carry when sum is 16 or more, etc. and _________ them, then sum them up • 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 16 1 Partial Products + 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 -1 0 SUBTRACTION THE EASY WAY 0 1 -0 0 1 1 -1 0 0 1 2.13 2.14 Modulo Arithmetic Taking the Negative CS:APP 2.3.3 • The primary difference between how humans and computers perform arithmetic is the finite • Question : Given a number in 2’s complement how _______________ of computers do we find its negative (i.e. -1 * X) – As humans we can use more digits (precision) as • Answer : By " __________________________ " needed – 0110 = +6 => -6 = 1010 – Computers can only used a _________ set of bits • Much like the odometer on your car once you go too – Operation defined as: many miles the values will wrap from 999999 to 000000 1. ______________________________ • Essentially all computer arithmetic is ___________ arithmetic 2. ______________________________ • If we have a width of w bits, then all operations are (i.e. finish with the same # of bits as we start with) 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 1 -32 16 8 4 2 1 2 • Invert (flip) each bit Original # = 0 -32 16 8 4 2 1 101010 Original number = -22 0000 (take the 1’s 010011 Original number = +19 Take the Take the 2’s complement 2’s complement complement) yields the negative of a number Bit flip is called the 1’s – 1’s become 0’s 2’s comp. of 0 is __ complement of a number Resulting number = +22 – 0’s become 1’s 3 Resulting number = -19 Taking the 2’s complement 1000 Original # = -8 • Add 1 (drop final again yields the original number (the operation is carry-out, if any) symmetric) Take the 2’s complement Back to original = -22 Negative of -8 is ___ Important: Taking the 2’s complement is equivalent to (i.e. no positive taking the negative (negating) equivalent, but this is not a huge problem) 2.17 2.18 Radix Complement 12 . 11 1 11 12 1 10 2 10 2 Clock Analogy 9 3 9 3 4-2 = 4+10 8 4 8 4 5 7 6 7 6 5 99 00 01 99 00 01 98 02 98 02 03 10’s complement 03 04-02 = 04 + 98 The same algorithms regardless of unsigned or signed . 04 . 04 . ADDITION AND SUBTRACTION 0000 0000 1111 0001 1111 0001 1110 0010 1110 0010 0011 2’s complement 0011 . 0100 0100 - 0010 = 0100 + 1110 . 0100 . When using modulo arithmetic , subtraction can always be converted to addition. 2.19 2.20 2’s Complement Addition/Subtraction 2’s Complement Addition 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 – Drop any final ______________ 0011 (3) 1101 (-3) • 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 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 #) unsigned and signed numbers • Drop any extra carry out – Add columns right to left 0011 (+3) 1101 (-3) • Examples: - 0010 (+2) - 1110 (-2) If unsigned If signed 1001 + 0011 2.23 2.24 Unsigned and Signed Subtraction Important Note • 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 If unsigned If signed • Thus we only need one set of 1100 (12) (-4) - 0010 (2) (2) _____________________________ to perform operations on both unsigned and signed numbers If unsigned If signed 2.25 2.26 CS:APP 2.3.1 Overflow 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 +15 -2 +2 0000 +1 1110 0010 1111 +14 0001 +2 -3 1110 0010 1101 0011 +3 +13 1101 +3 5 + 7 = +12 0011 -4 1100 0100 Plus 7 +4 10 + 7 = 17 +12 1100 0100 +4 -6 + -4 = -10 1011 0101 -5 +5 1010 1011 10 0101 With 4-bit 2’s complement 0110 With 4-bit unsigned numbers we +11 +5 numbers we can only represent -6 1001 0111 +6 can only represent 0 – 15. Thus, 1010 0110 1000 we say overflow has occurred. -8 to +7. Thus, we say overflow +7 +10 1001 0111 +6 -7 has occurred. -8 1000 +9 +7 +8 Overflow occurs when you cross this discontinuity 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 cannot be represented with the given number of of bits. 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) 0001 (17) (+1) 1011 (11) (-5) Desired 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) 0 1 1 0 MULTIPLICATION AND DIVISION 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 2.33 2.34 Binary Division Unsigned Multiplication Review • Dividing two n-bit numbers may yield an • Same rules as decimal multiplication n-bit quotient and n-bit remainder • Multiply each bit of Q by M shifting as you go • Division operations on a modern processor can take • An m-bit * n-bit mult.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages14 Page
-
File Size-