Ahmad Almulhem, KFUPM 2009 COE 202: Digital Logic Design Number Systems Part 4 Dr. Ahmad Almulhem ahmadsm AT kfupm Phone: Office:
Ahmad Almulhem, KFUPM 2009 Objectives 1.Overflow 2.Shift operations 3.Binary codes
Ahmad Almulhem, KFUPM 2009 Overflows Number’s sizes in computers are fixed Overflows can occur when the result of an operation does not fit. Q: When can an overflow occur? Unsigned numbersSigned numbers Subtracting two numbersAdding a positive number to a negative number Adding two numbersAdding two negative numbers or two positive numbers
Ahmad Almulhem, KFUPM 2009 Overflow (2’s Complement) Q: Add +5 and +4 in 2’s complement. A: An overflow happened. The correct answer +9 (1001) cannot be represented by 4 bits. Detection: 1.Adding two positive numbers result in a negative number! 2.Carry in sign bit is different from carry out of sign bit Solution: Use one more bit, extend the sign bit ??
Ahmad Almulhem, KFUPM 2009 Overflow (2’s Complement) Q: Add -5 and -4 in 2’s complement. A: An overflow happened. The correct answer -9 (10111) cannot be represented by 4 bits. Detection: 1.Adding two negative numbers result in a positive number! 2.Carry in sign bit is different from carry out of sign bit Solution: Use one more bit, extend the sign bit ??
Ahmad Almulhem, KFUPM 2009 Range Extensions To extend the representation of a 2’s complement number possibly for storage and use in a larger-sized register If the number is positive, pad 0’s to the left of the integral number (sign bit extension), and 0’s to the right of the fractional number If the number is negative, pad 1’s to the left of the integral number (sign bit extension), and 0’s to the right of the fractional number bit register (+9) bit register (-9) 9-bit register (-9) – sign bit extended 9-bit register (+9) – sign bit extended
Ahmad Almulhem, KFUPM 2009 Arithmetic Shift A binary number can be shifted right or left To shift an unsigned numbers (right or left), pad with 0s. Example: Left Shift Q1: What is the effect of left-shifting? Q2: What is the effect of right-shifting?
Ahmad Almulhem, KFUPM 2009 Arithmetic Shift To shift a signed number – Left-Shift: pad with 0s – Right-Shift: Extend sign bit Example (2’s complement): Right Shift In General - left-shifting = multiply by r - right-shifting = divide by r
Ahmad Almulhem, KFUPM 2009 Arithmetic Shifts
Ahmad Almulhem, KFUPM 2009 Binary Codes Human communicating with computers – Humans understand decimal – Computers understands binary Communication mode is decimal Binary codes are used to translate individual digits of a given number (generally in base 10) to binary format based on given rules Computers …. Human 1234 ….
Ahmad Almulhem, KFUPM 2009 Binary Coded Decimal (BCD) BCD Code uses 4 bits to represent the 10 decimal digits {0 to 9} 6 BCD codes unused The weights of the individual positions of the bits of a BCD code are: 2 3= 8, 2 2 =4, 2 1 =2, 2 0 =1
Ahmad Almulhem, KFUPM 2009 BCD Addition
Ahmad Almulhem, KFUPM 2009 Gray Code Gray code represents decimal numbers 0 to 15 using bit codes Gray codes of two adjacent decimal numbers differ by only one bit Example: (5) 10 = 0111 (6) 10 = 0101 (7) 10 = 0100
Ahmad Almulhem, KFUPM 2009 ASCII Code (Alphanumeric) American Standard Code for Information Interchange (ASCII): includes binary definitions for 128 characters that include the English alphabets, special symbols and control characters. ASCII codes have a length of 7 bits Examples: A = (65) 10 = B = (66) 10 = Z = (90) 10 = a = (97) 10 = b = (98) 10 = z = (122) 10 =
Ahmad Almulhem, KFUPM 2009 Parity Bits
Ahmad Almulhem, KFUPM 2009 Codes Summary Bits are bits – Modern digital devices represent everything as collections of bits – A computer is one such digital device You can encode anything with sufficient 1’s and 0’s – Text (ASCII) – Computer programs (C code, assembly code, machine code) – Sound (.wav,.mp3,...) – Pictures (.jpg,.gif,.tiff)
Ahmad Almulhem, KFUPM 2009 Numbers have fixed sizes in a computer, therefore overflow can occur in any representation. Detection of overflow by sign or carry-in carry-out of the sign bit. To extend a signed number, extend the sign Shift operations (right, left) Binary codes (BCD, gray code, ASCII) Conclusions