2.1 Unit 2 Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting)
2.2 Skills & Outcomes • You should know and be able to apply the following skills with confidence – Perform addition & subtraction in unsigned & 2's complement system – Determine if overflow has occurred – 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 UNSIGNED BINARY ARITHMETIC
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 2 or more rather than 10 or more (decimal) – We borrow 2’s not 10’s from other columns • 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 Binary Addition • In decimal addition we carry when the sum is 10 or more • In binary addition we carry when the sum is 2 or more • Add bits in binary to produce a sum bit and a carry bit 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.6 Binary Addition & Subtraction 1 1 1 0 0 0 1 1 1 (7) 1 0 1 0 1 (10) 1 + 0 0 1 1 (3) - 0 1 0 1 (5) 1 0 1 0 (10) 0 1 0 1 (5) 8 4 2 1 8 4 2 1
2.7 Binary Addition 110 0110 (6) 8 4 2 1 + 0111 (7) 1101 (13)
2.8 Binary Addition 1 2 0 10 0 0 0110 (6) 0110 (6) 1 + 1 + 0111 (7) + 0111 (7) + 1 01 1101 (13) 1101 (13) 10 carry bit sum bit carry bit sum bit 110 1 3 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
2.9 Hexadecimal Arithmetic • Same style of operations – Carry when sum is 16 or more, etc. 1 1 4 D 16 13+5 = 18 10 = 1 2 16 16 1 + B 5 16 1+4+11 = 16 10 = 1 0 16 16 1 1 0 2 16
2.10 Binary Multiplication • Like decimal multiplication, find each partial product and shift them, then sum them up • Multiplying two n- bit numbers yields at most a 2*n -bit product 0 1 1 0 (6) * 0 1 0 1 (5) 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
2.11 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 (5 r.1) 10 0 1 0 1 r.1 (2) 10 10 1 0 1 1 (11) 10 -1 0 0 1 -0 0 1 1 -1 0 0 1
2.12 "Taking the 2's complement" SUBTRACTION THE EASY WAY
2.13 Modulo Arithmetic • The primary difference between how humans and computers perform arithmetic is the finite precision of computers – As humans we can use more digits (precision) as needed – Computers can only used a finite set of bits • Much like the odometer on your car once you go too many miles the values will wrap from 999999 to 000000 • Essentially all computer arithmetic is modulo arithmetic • If we have a width of w bits, then all operations are module 2 w • 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 subtract hours
2.14 Taking the Negative CS:APP 2.3.3 • Question : Given a number in 2’s complement how do we find its negative (i.e. -1 * X) • Answer : By " taking the 2’s complement " – 0110 = +6 => -6 = 1010 – Operation defined as: 1. Flip/invert/not all the bits (1’s complement) 2. Add 1 and drop any carry (i.e. finish with the same # of bits as we start with) – See next slides for example
2.15 Taking the 2’s Complement • Invert (flip) each bit -32 16 8 4 2 1 010011 (take the 1’s Original number = +19 complement) – 1’s become 0’s Bit flip is called the 1’s 101100 complement of a number – 0’s become 1’s + 1 • Add 1 (drop final 101101 Resulting number = -19 carry-out, if any) Important: Taking the 2’s complement is equivalent to taking the negative (negating)
2.16 Taking the 2’s Complement 1 2 -32 16 8 4 2 1 0000 Original # = 0 101010 Original number = -22 1111 Take the 010101 Take the 2’s complement 2’s complement + 1 yields the negative of a + 1 number 0000 2’s comp. of 0 is 0 010110 Resulting number = +22 3 1000 Taking the 2’s complement Original # = -8 101001 again yields the original number (the operation is 0111 + 1 Take the symmetric) 2’s complement + 1 101010 Back to original = -22 1000 Negative of -8 is -8 (i.e. no positive equivalent, but this is not a huge problem)
2.17 The same algorithms regardless of unsigned or signed ADDITION AND SUBTRACTION
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 5 7 6 6 00 01 00 01 99 99 98 98 02 02 10’s complement 03 03 04-02 = 04 + 98 . . 04 04 . . . . . . 0000 0001 0000 0001 1111 1111 1110 1110 0010 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’s Complement Addition/Subtraction CS:APP 2.3.1 • Addition CS:APP 2.3.2 – Sign of the numbers do not matter – Add column by column – Drop any final carry-out • The secret to modulo arithmetic • Subtraction – Any subtraction (A-B) can be converted to addition (A + - B) by taking the 2’s complement of B – (A-B) becomes (A + ~B + 1) – Drop any carry-out • The secret to modulo arithmetic
2.20 2’s Complement Addition • No matter the sign of the operands just add as normal • Drop any extra carry out 0000 0000 0011 (3) 1101 (-3) + 0010 (2) + 0010 (2) 0101 (5) 1111 (-1) Drop final 1110 1100 carry-out 0011 (3) 1101 (-3) + 1110 (-2) + 1110 (-2) 0001 (1) 1011 (-5)
2.21 Unsigned and Signed Addition • Addition process is the same for both unsigned and signed numbers – Add columns right to left • Examples: 1 1 If unsigned If signed 1001 (9) (-7) + 0011 (3) (3) 1100 (12) (-4)
2.22 2’s Complement Subtraction • Take the 2’s complement of the subtrahend (bottom #) and add to the original minuend (top #) • Drop any extra carry out 0011 (+3) 1101 (-3) - 0010 (+2) - 1110 (-2) 1111_ 1_ Drop final carry-out 0011 1101 1101 0001 Bit flip of +2 Bit flip of -2 + 1 + 1 Add 1 Add 1 0001 1111
2.23 Unsigned and Signed Subtraction • Subtraction process is the same for both unsigned and signed numbers – Convert A – B to A + Comp. of B – Drop any final carry out • Examples: 11_1_ If unsigned If signed 1100 (12) (-4) 1100 A - 0010 (2) (2) 1101 ~B + 1 Add 1 (10) (-6) 1010 If unsigned If signed
2.24 Important Note • Almost all computers use 2's complement because… • The same addition and subtraction algorithm can be used on unsigned and 2's complement (signed) numbers • Thus we only need one set of circuitry (HW component) to perform operations on both unsigned and signed numbers
2.25 OVERFLOW
2.26 Overflow CS:APP 2.3.1 CS:APP 2.3.2 • Overflow occurs when the result of an arithmetic operation is too large to be represented with the given number of bits • Conditions and tests to determine overflow depend on the system being used – Different algorithms for detecting overflow based on unsigned or signed
2.27 Unsigned Overflow Overflow occurs when you cross this discontinuity 0 +15 +1 0000 1111 0001 +14 +2 1110 0010 +13 +3 1101 0011 Plus 7 10 + 7 = 17 +12 1100 0100 +4 With 4-bit unsigned numbers we 10 1011 0101 +11 can only represent 0 – 15. Thus, +5 1010 0110 we say overflow has occurred. +10 1001 0111 +6 1000 +7 +9 +8
2.28 2’s Complement Overflow 0 -1 +1 0000 1111 0001 -2 +2 1110 0010 -3 +3 1101 0011 5 + 7 = +12 -4 1100 0100 +4 -6 + -4 = -10 1011 0101 -5 +5 With 4-bit 2’s complement 1010 0110 numbers we can only represent -6 1001 0111 +6 -8 to +7. Thus, we say overflow 1000 +7 -7 has occurred. -8 Overflow occurs when you cross this discontinuity
2.29 Overflow in Addition • Overflow occurs when the result of the addition cannot be represented with the given number of bits. • Tests for overflow: – Unsigned: if Cout = 1 [result smaller than inputs] – Signed: if p+p=n or n+n=p [result has inappropriate sign] 1 1 If unsigned If signed 0 1 If unsigned If signed 1101 (13) (-3) 0110 (6) (6) + 0100 (4) (4) + 0101 (5) (5) 0001 (17) (+1) 1011 (11) (-5) Overflow No Overflow No Overflow Overflow Cout = 1 n + p Cout = 0 p + p = n
2.30 Overflow in Subtraction • Overflow occurs when the result of the subtraction cannot be represented with the given number of bits. • Tests for overflow: – Unsigned: if Cout = 0 [expect negative result] – Signed: if p+p=n or n+n=p [result has inappropriate sign] 0111_ If unsigned If signed 0111 (7) (7) 0111 A - 1000 (8) (-8) 0111 1’s comp. of B (-1) (15) + 1 Add 1 (15) (-1) 1111 Desired Results If unsigned If signed Overflow Overflow Cout = 0 p + p = n
2.31 MULTIPLICATION AND DIVISION
Recommend
More recommend