170 likes | 321 Views
Dr. Ahmad Almulhem Email: ahmadsm AT kfupm Phone: 860-7554 Office: 22-324. COE 202: Digital Logic Design Number Systems Part 4. Objectives. Overflow Shift operations Binary codes. Overflows. Number’s sizes in computers are fixed
E N D
Dr. Ahmad Almulhem Email: ahmadsm AT kfupm Phone: 860-7554 Office: 22-324 COE 202: Digital Logic DesignNumber SystemsPart 4 Ahmad Almulhem, KFUPM 2009
Ahmad Almulhem, KFUPM 2009 Objectives • Overflow • Shift operations • Binary codes
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? 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: • Adding two positive numbers result in a negative number! • Carry in sign bit is different from carry out of sign bit • Solution: • Use one more bit, extend the sign bit 0 1 0 0 0101 + 510 + 0100 + 410 0 1001 - 710?? 0 0 1 0 00101 + 510 + 00100 + 410 01001 + 910 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: • Adding two negative numbers result in a positive number! • Carry in sign bit is different from carry out of sign bit • Solution: • Use one more bit, extend the sign bit 1 0 0 0 1011 - 510 + 1100 - 410 1 0111 + 710?? 1 1 0 0 11011 - 510 + 11100 - 410 10111 - 910 Ahmad Almulhem, KFUPM 2009
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 0 1 0 0 1 0 0 0 0 0 1 0 0 1 9-bit register (+9) – sign bit extended 5-bit register (+9) 1 0 1 1 1 1 1 1 1 1 0 1 1 1 9-bit register (-9) – sign bit extended 5-bit register (-9)
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 0001 110 0010 210 0100 410 1000 810 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 1000 - 810 1100 - 410 1110 - 210 1111 - 110 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 Human 1234 …. Computers 01011 ….
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: 23=8, 22=4, 21=2, 20=1 Ahmad Almulhem, KFUPM 2009
Ahmad Almulhem, KFUPM 2009 BCD Addition
Ahmad Almulhem, KFUPM 2009 Gray Code • Gray code represents decimal numbers 0 to 15 using 16 4-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 = 100 0001 • B = (66)10 = 100 0010 • Z = (90)10 = 101 1010 • a = (97)10 = 110 0001 • b = (98)10 = 110 0010 • z = (122)10 = 111 1010
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 Conclusions • 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)