1 2 Semester Transition Point EE 109 Unit 11 Binary Arithmetic At this point we are going to start to transition in our class to look more at the hardware organization and the low-level software that is produced by compilers This is where HW really meets SW We need to finish off some topics of how the HW carries out operations on numbers 3 4 Binary Arithmetic 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 )
5 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 Add bits in binary to produce a sum bit and a carry bit 0 1 1 1 + 0 0 1 1 1 0 1 0-0 1 0 1 0 0 1 1 1 + 0 + 1 + 0 + 1 00 01 01 10 no need to carry sum bit no need to carry sum bit no need to carry sum bit carry 1 into next column of bits sum bit 7 8 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 -bit product 0 1 1 0 * 0 1 0 1 Hexadecimal Arithmetic Same style of operations Carry when sum is 16 or more, etc. 4 D 16 + B 5 16
9 Taking the Negative 10 Given a number in signed magnitude or 2 s complement how do we find its negative (i.e. -1 * X) Signed Magnitude: "Taking the 2's complement" SUBTRACTION THE EASY WAY 2 s complement: Operation defined as: 1. 2. 11 12 Taking the 2 s Complement Taking the 2 s Complement Invert (flip) each bit (take the 1 s complement) 1 s become 0 s 0 s become 1 s Add 1 (drop final carry-out, if any) -32 16 8 4 2 1 010011 Original number = +19 Bit flip is called the 1 s complement of a number Resulting number = -19 1-32 16 8 4 2 1 2 Original number = 101010 Take the 2 s complement yields the negative of a number Resulting number = Back to original = 3 0000 1000 Original # = 0 Take the 2 s complement 2 s comp. of 0 is Original # = -8 Take the 2 s complement Negative of -8 is Important: Taking the 2 s complement is equivalent to taking the negative (negating) (i.e. no positive equivalent, but this is not a huge problem)
2 s Complement System Facts Normal binary place values but MSB has negative weight MSB determines sign of the number 0 = positive / 1 = negative Special Numbers 0 = All 0 s (00 00) -1 = All 1 s (11 11) Max Positive = 0 followed by all 1 s (011..11) Max Negative = 1 followed by all 0 s (100 00) To take the negative of a number (e.g. -7 => +7 or +2 => -2), requires taking the complement 2 s complement of a # is found by flipping bits and adding 1 1001 0110 + 1 0111 x = -7 Bit flip (1 s comp.) Add 1 -x = -(-7) = +7 13 2 s Complement Addition/Subtraction Addition Sign of the numbers do not matter Subtraction Any subtraction (A-B) can be converted to by (A-B) becomes Drop any carry-out 14 2 s Complement Addition 15 Unsigned and Signed Addition 16 No matter the sign of the operands just add as normal Drop any extra carry out 0011 + 0010 + 0010 Addition process is the same for both unsigned and signed numbers Add columns right to left Examples: 0011 + 1110 + 1110 1001 + 0011
2 s Complement Subtraction Take the 2 s complement of the subtrahend and add to the original minuend Drop any extra carry out 0011(+3) - 0010(+2) (-3) - 1110(-2) 17 Unsigned and Signed Subtraction Subtraction process is the same for both unsigned and signed numbers Convert A B to A + 2 s Comp. of B Drop any final carry out Examples: 18 1100-0010 Important Note 19 20 Almost all computers use 2's complement because The same addition and subtraction can be used on unsigned and 2's complement (signed) numbers Thus we only need to perform operations on both unsigned and signed numbers OVERFLOW
21 22 Overflow Unsigned Overflow Overflow occurs when the result of an arithmetic operation is Conditions and tests to determine overflow depend on 10 + 7 = 17 With 4-bit unsigned numbers we can only represent 0 15. Thus, we say overflow has occurred. Overflow occurs when you cross this discontinuity +12 +14 +13 1100 1011 +11 +10 1110 1010 +15 +9 1111 Plus 7 1001 10 0 0000 1000 +8 +1 0001 +2 0010 0011 +3 0100 +4 0101 +5 0110 0111 +6 +7 23 24 2 s Complement Overflow Overflow in Addition 5 + 7 = +12-6 + -4 = -10 With 4-bit 2 s complement numbers we can only represent -8 to +7. Thus, we say overflow has occurred. -4-3 -5-2 1100 1011-6 1110 1010-1 -7 1111 1001 0 0000 1000-8 0001 0111 +1 +7 0010 0110 0011 +2 0101 0100 +6 +3 +5 +4 Overflow occurs when the result of the addition cannot be represented with the given number of bits. Tests for overflow: Unsigned: Signed: + 0100 0110 + 0101 Overflow occurs when you cross this discontinuity
Overflow in Subtraction 25 26 Overflow occurs when the result of the subtraction cannot be represented with the given number of bits. Tests for overflow: Unsigned: Signed: 0111-1000 TRANSLATING SIGNED OR UNSIGNED HEXADECIMAL Translating Hexadecimal 27 Taking the 16 s Complement 28 Hex place values (16 2, 16 1, 16 0 ) can ONLY be used if the number is If hex represents unsigned binary 1. B2 hex = If hex represents signed value (2 s comp.) 1. 2. 3. Hex Binary Sign 0 = 0000 = Pos. 1 = 0001 = Pos. 2= 0010 = Pos. 3 = 0011 = Pos. 4 = 0100 = Pos. 5 = 0101 = Pos. 6 = 0110 = Pos. 7 = 0111 = Pos. 8 = 1000 = Neg. 9 = 1001 = Neg. A = 1010 = Neg. B = 1011 = Neg. C = 1100 = Neg. D = = Neg. E = 1110 = Neg. F = 1111 = Neg. Taking the 2 s complement of a binary number yields its negative and is accomplished by taking the complement (bit flip) and adding 1 Taking the 16 s complement of a hex number yields its negative and is accomplished by finding the 15 s complement and adding 1 15 s complement is found by subtracting each digit of the hex number from 16 Original value B2: 16 s comp. of B2:
Translating Hexadecimal Given 6C hex If it is unsigned: 6C hex = If it is signed Determine the sign by looking at MSD 0-7 hex has a in the MSB [i.e. ] 8-F hex has a in the MSB [i.e. ] Thus, 6C is 6C hex = 29 Translating Hexadecimal Given B2 hex If it is unsigned B2 hex = If it is signed Determine the sign: Take the 16 s complementand apply hex place values to find the neg. number s magnitude 30 Finding the Value of Hex Numbers 31 Hex Addition and Overflow 32 8A hex representing a signed (2 s comp.) value Step 1: Determine the sign: Step 2: Take the 16 s comp. to find magnitude Step 3: Apply hex place values ( 16 = 10 ) Step 4: Final value: B2 hex = 10 7C hex representing a signed (2 s comp.) value Step 1: Determine the sign: Step 2: Apply hex place values (7C 16 = 10 ) 7C hex representing an unsigned value = 10 Same rules as in binary Add left to right Drop any carry (carry occurs when sum > F 16 ) Same addition overflow rules Unsigned: Signed: 7AC5 + C18A 6C12 + 549F
Hex Subtraction and Overflow 33 Same rules as in binary Convert A B to A + Comp. of B Drop any final carry out Same subtraction overflow rules Unsigned: Signed: B1ED - 76FE 0001-0002