ECE 233 Intro to Digital Systems Topics Binary Arithmetic ECE 233 Intro to Digital Systems 1 Administrative Lab 1 Checkpoint 2 due Tuesday 12 05 23 Homework 1 due Monday 12 04 23 ECE 233 Intro to Digital Systems 2 Review Computers use binary not decimal to represent values Binary digits can only have two values 0 or 1 A single binary digit is called a bit B2B1B0 B2 22 B1 21 B0 20 0 Bi 1 Based on this we can conclude that computers also do arithmetic in binary ECE 233 Intro to Digital Systems 3 Binary Addition Binary addition works the same as base 10 in that if you 1 Add the digits in a column together 2 If you exceed the max value for a digit you have a carry to the next digit Key difference is the max value for a binary digit is 1 So 1 1 0 i e the sum is 0 with a carry of 1 Octal and Hexadecimal are similar only the max value for each digit is 7 and F 15 Note You can always double check by converting back to decimal ECE 233 Intro to Digital Systems 4 Binary Addition Examples Unsigned Example 0112 0112 sum carry 1 1 011 011 1 0 1 102 1 1 210 1 1 1 112 1 0 0 12 Example 1002 1002 1 100 100 000 0 0 02 0 0 02 1 1 102 Decimal Check 0112 310 310 310 610 1102 610 Decimal Check 1002 410 410 410 810 0002 010 Wait so 4 4 0 What s going on here ECE 233 Intro to Digital Systems 5 Binary Addition Overflow Overflow is when an addition exceeds the capacity we ve allocated for The result overflows our available digits In the previous example we had two 3 bit values and only have 3 bits for the result i e 1002 1002 0002 With the carry bit i e a fourth bit we would have the correct result 10002 or 810 but we only have 3 bits Another example Perform the following 3 bit addition 1112 1112 o 1112 1112 1102 or 710 710 610 instead of 1410 o We need 4 bits to represent 14 ECE 233 Intro to Digital Systems 6 In class Activity Unsigned Binary Addition Perform the following 4 bit unsigned binary additions Do they overflow 1 01002 01102 Answer 10102 or 1010 no it does not overflow 2 10002 11002 Answer 01002 or 410 yes it overflows 3 01112 00102 Answer 10012 or 910 no it does not overflow 4 01012 11002 Answer 00012 or 110 yes it overflows Again alternatively could convert back to decimal and see if it makes sense ECE 233 Intro to Digital Systems 7 2 s Complement Addition 2 s Complement method represents the signed binary 2 s Complement Addition works the same as unsigned number binary addition Key difference is 1 You can have negative numbers i e the sign matters Remember for 2 s Complement the first digit MSB is negative 2 Overflow is not as simple as is there a carry out Simple check is convert back to decimal and see if the answer makes sense ECE 233 Intro to Digital Systems 8 sum carry 2 s Complement Examples Example 10012 00112 assume 2 s Complement 11 1001 0011 010 1 1 1 102 1 1 102 1 0 0 12 1 0 12 Decimal Check 10012 710 00112 310 710 310 410 11002 410 Example 11112 11112 assume 2 s Complement 11 1 1 Decimal Check 1111 11112 110 1111 110 110 210 01 11102 210 1 1 102 1 1 1 112 1 1 1 112 1 1 1 112 carry sum 1 1 Again a carry does not always mean overflow for 2 s complement ECE 233 Intro to Digital Systems 9 Binary Subtraction Subtraction is a little bit more tricky Computers don t actually have a subtracting unit Then what do they do Subtraction is really just adding one number with the negated value of the 2nd number Remember how to negate a 2 s complement number o Invert all the bits and add 1 Similarly computers just negate the 2nd number and add This is why 2 s Complement is important It allows us to represent negative numbers in binary ECE 233 Intro to Digital Systems 10 Binary Subtraction Examples 2 s Complement Example 01112 00112 1 First step is to negate the 2nd number Invert 0011 1100 Add 1 1100 1 1101 2 Now add the two like normal 11 1 1 0111 1101 00 1 1 102 1 1 102 1 1 1 112 1 1 102 carry sum 0 1 Decimal Check 01112 710 00112 310 710 310 410 01002 410 Again note that even though you have a carry out the result did not overflow We can represent the result with 4 bits ECE 233 Intro to Digital Systems 11 In class Activity 2 s Complement Arithmetic Perform the following 4 bit 2 s Complement Arithmetic Do they overflow 1 01002 01102 Answer 10102 or 610 yes it overflows 2 01012 11002 Answer 00012 or 110 no it does not overflow 3 01012 00102 Answer 00112 or 310 no it does not overflow 4 10112 01002 Answer 01112 or 710 yes it overflows ECE 233 Intro to Digital Systems 12 Revisiting Overflow Here are some tricks to see if a result overflows 1 For unsigned arithmetic o If Carry OutN 1 for addition or Carry OutN 0 for subtraction you have overflow 2 For 2 s Complement arithmetic o If Carry OutN Carry inN 1 you have overflow Carry out Cn Carry in Cn 1 Unsigned Overflow 2 s Complement Overflow Subtract Add Subtract Add 0 0 1 1 0 1 0 1 Yes Yes Yes Yes Yes Yes Yes Yes ECE 233 Intro to Digital Systems 13 Next class Logic Gates Lab 1 Checkpoint 2 due Tuesday 12 05 23 Homework 1 due Monday 12 04 23 ECE 233 Intro to Digital Systems 14
View Full Document