EE 260: Introduction to Digital Design Number Systems Yao Zheng Department of Electrical Engineering University of Hawaiʻi at Mānoa Overview n Digital Computers n Number Systems n Representations n Conversions n Arithmetic Operations n Decimal Codes n Alphanumeric Codes 1 2 n Major characteristic Digital Systems n manipulation of discrete elements of information (any set restricted to a finite # of elements) n e.g. 10 decimal digits, 26 letters n Discrete elements (in digital systems) can be represented by signals (physical quantities). n Most common signals: electrical (voltage, current) Information Representation n Binary signals (2 discrete values) n 0 and 1 (LOW and HIGH, FALSE and TRUE) n Binary quantity: binary digit/bit n Information: group of bits/words (size: 8, 16, 32, 64, ) n Digital hardware computes binary functions of binary numbers: n Combinational (memoryless) n Sequential (using memory) 3 4 Number Systems n Representation of numbers n Radix: base, the primitive unit for group of numbers, e.g. for decimal arithmetic radix=10 ( base 10) n For every system, we need arithmetic operations (addition, subtraction, multiplication) n Also, conversion from one base to the other Number Systems (cont.) n Most common number systems for computers: n Binary (r = 2) n Octal (r = 8) n Hexadecimal (r = 16) 5 6 Chapter 2: Number Systems 1
Number Systems - Decimal n base 10 (radix is 10) n 10 digits: 0..9 n (251.3) 10 = 2*10 2 + 5*10 1 + 1*10 0 + 3*10-1 n Note:. is called the radix point (decimal point for base 10) Number Systems Decimal (cont.) n In general, a decimal number with n digits to the left of the decimal point, and m digits to the right of the decimal point is written as: A n-1 A n-2 A 1 A 0. A -1 A -2 A -m+1 A -m where A i is a coefficient between 0..9, and i denotes the weight (=10 i ) of A i. 7 8 Number Systems Decimal (cont.) The value of A n-1 A n-2 A 1 A 0. A -1 A -2 A -m+1 A -m is calculated by Number Systems General n base r (radix r) n r digits n N = A n-1 *r n-1 + A n-2 *r n-2 + + A 1 *r + A 0 + A -1 *r -1 + A -2 *r -2 + + A -m *r -m å i=n-1..0 (A i * 10 i ) + å i=-m..-1 (A i * 10 i ) Most Significant Bit (MSB) Least Significant Bit (LSB) 9 10 Number Systems General (cont.) n e.g. let r = 8 (312.5) 8 = 3*8 2 + 1*8 1 + 2*8 0 + 5*8-1 = (202.625) 10 n Conversion from n-ary (any system with radix n) to decimal follows similar process as above Binary Numbers-Base 2 n Computers represent all data as strings of bits, each bit being either 0 or 1 n base 2, with 2 digits: 0 and 1 n e.g. (101101.10) 2 = 1*2 5 + 0*2 4 + 1*2 3 + 1*2 2 + 0*2 1 + 1*2 0 + 1*2-1 + 0*2-2 (in decimal) = 32 + 0 + 8 + 4 + 0 + 1 + ½ + 0 = (45.5) 10 11 12 Chapter 2: Number Systems 2
Binary Numbers-Base 2 (cont.) Powers of two n e.g. (1001.011) 2 = 1*2 3 + 0*2 2 + 0*2 1 + 1*2 0 + 0*2-1 + 1*2-2 + 1*2-3 (in decimal) = 8 + 1 + 0.25 + 0.125 = (9.375) 10 Memorize at least through 2 16 13 14 Octal Numbers-Base 8 n base 8, with 8 digits: 0..7 n e.g. (762) 8 = 7*8 2 + 6*8 1 + 2*8 0 (in decimal) = 448 + 48 + 2 = (498) 10 Hexadecimal Numbers-Base 16 n r = 16 n Digits (convention): 0..9, A, B, C, D, E, F n A=10, B=11,, F = 15 n e.g. (3FB) 16 = 3*16 2 + 15*16 1 + 11*16 0 (in decimal) = 768 + 240 + 11 = (1019) 10 15 16 Base Conversions n Any base r decimal Easy! (already covered, see slides 13-14, 16-17, 19-20) n Decimal Binary n Octal «Binary n Hex «Binary n Decimal Any base r Decimal to Binary Let N be a decimal number. 1. Find the greatest number that is a power of 2 and when subtracted from N it produces a positive difference N 1 2. Put a 1 in the MSB 3. Repeat Step 1, starting from N 1 and finding difference N 2. Put a 1 in the corresponding bit. Stop when the difference is zero. 17 18 Chapter 2: Number Systems 3
Decimal to Binary (cont.) e.g. N = (717) 10 717 512 = 205 = N 1 512 = 2 9 205 128 = 77 = N 2 128 = 2 7 77 64 = 13 = N 3 64 = 2 6 13 8 = 5 = N 4 8 = 2 3 5 4 = 1 = N 5 4 = 2 2 1 1 = 0 = N 6 1 = 2 0 à (717) 10 = 2 9 + 2 7 + 2 6 + 2 3 + 2 2 + 2 0 Binary to Octal and Hex n Octal: 8 = 2 3 à every 3 binary bits convert 1 octal n Hex: 16 = 2 4 à every 4 binary bits convert 1 hex = ( 1 0 1 1 0 0 1 1 0 1) 2 19 20 Binary «Octal Binary «Hex (011 010 101 000. 111 101 011 100) 2 ( 0110 1010 1000. 1111 0101 1100 ) 2 ( 3 2 5 0. 7 5 3 4 ) 8 ( 6 A 8. F 5 C ) 16 21 22 Octal «Hex Go through Binary! Hex à Binary à Octal Octal à Binary à Hex Convert Decimal to any base r n Integer part: Divide by the base, keep track of remainder, and read-up n e.g. (153) 10 = (? ) 8, r = 8 153 / 8 = 19 + 1/8 rem = 1 LSB 19 / 8 = 2 + 3/8 rem = 3 2 / 8 = 0 + 2/8 rem = 2 MSB stop à (153) 10 = ( 231) 8 23 24 Chapter 2: Number Systems 4
Convert Decimal to any base r n Fractional part: Multiply by the base, keep track of integer part, and read-down n e.g. (0.78125) 10 = (? ) 16, r = 16 0.78125*16 = 12.5 integer = 12 = C MSB 0.5 * 16 = 8.0 integer = 8 = 8 LSB stop à (0.78125) 10 = (0.C8) 16 Recap: Number Systems n Positional number systems: n D = d1 d0. d-1 d-2 = d110 1 + d010 0 + d-110-1 + d-210-2 n Here 10 is called base or radix. n General form: (r>1 integer) MSB LSB p 1 D å - = d r i i= -n n 10011 2 = 1*16 + 0*8 + 0*4 +1*2 + 1*1 = 19 10 i 25 26 Recap: Octal and Hexadecimal Numbers Recap: Positional Number System Conversions n Base 8 and 16 provide convenient shorthand representation for multi-bit numbers in a digital system. n Octal digit è 3-bit string (0-7) n Hexadecimal digit è 4-bit string (0-9, A-F) n 100011001110 2 = 4316 8 = 8CE 16 n 10.1011001011 2 = 010.101 100 101 100 2 = 2.5454 8 n = 0010.1011 0010 1100 2 = 2.B2C 16 n In hexadecimal systems 2 digits represent an 8- bit byte. 27 n Base-r-to-decimal conversions: n (dp-1 d1 d0. d-1 d-2 d-n )r n r: base/radix n p: digits to the left of radix point n n: digits to the right of radix point n Eg: n 436.5 8 = 4*8 2 + 3*8 1 + 6*8 0 + 5*8-1 = 286.625 10 p 1 D å - = d r i i= -n i 28 Recap: Positional Number System Conversions n Decimal-to-base-r conversion: n 179 / 2 = 89 remainder 1 (LSB) / 2 = 44 rem 1 / 2 = 22 rem 0 / 2 = 11 rem 0 / 2 = 5 rem 1 / 2 = 2 rem 1 / 2 = 1 rem 0 / 2 = 0 rem 1 (MSB) n Addition Addition and Subtraction of Nondecimal Numbers 101111000 X 190 10111110 Y 141 10001101 + X+Y 331 101001011 C in/b in X Y C out S B out D 0 0 0 0 0 0 0 0 0 1 0 1 1 1 0 1 0 0 1 0 1 0 1 1 1 0 0 0 1 0 0 0 1 1 1 1 0 1 1 0 1 0 1 1 0 1 0 0 0 1 1 1 1 1 1 1 n Hence, 179 10 = 10110011 2 29 30 Chapter 2: Number Systems 5
Binary Arithmetic Operations: Addition n Follow same rules as in decimal addition, with the difference that when sum is 2 indicates a carry (not a 10) n Learn new carry rules n 0+0 = 0c0 (sum 0 with carry 0) n 0+1 = 1+0 = 1c0 n 1+1 = 0c1 n 1+1+1 = 1c1 Carry 1 1 1 1 1 0 Augend 0 0 1 0 0 1 Addend 0 1 1 1 1 1 Result 1 0 1 0 0 0 Binary Arithmetic Operations: Addition (cont.) n Half addition (rightmost bit position, aka LSB): only 2 bits are added, yielding a sum and a carry n Full addition (remaining positions): three bits are added, yielding a sum and a carry n In Chapter 8, we ll see many different hardware implementations of half-adders and full-adders 31 32 Binary Arithmetic Operations: Subtraction n Learn new borrow rules n 0-0 = 1-1 = 0b0 (result 0 with borrow 0) n 1-0 = 1b0 n 0-1 = 1b1 n Borrow 1 1 0 0 Minuend 1 1 0 1 1 Subtrahend 0 1 1 0 1 Result 0 1 1 1 0 n Binary subtraction: (borrow, difference bits) B out 001111100 Minuend X 229 11100101 Subtrahend Y 46 00101110 - Difference X-Y 183 10110111 n Use binary subtraction to compare numbers. If X- Y produces a borrow out at the most significant bit, then X is less than Y. 33 34 Representation of Negative Numbers n Signed-Magnitude System/Representation: n MSB: sign bit, 0: plus, 1: minus 01010101 2 = +85 10 11010101 2 = -85 10 01111111 2 = +127 10 11111111 2 = -127 10 00000000 2 = +0 10 10000000 2 = -0 10 n n-bit signed integer lies within (2 n-1-1) through +(2 n-1-1) with two representations of zero. 35 n Signed-magnitude adder n If signs are same else add magnitudes, sign is same compare magnitudes, subtract smaller from the larger, sign is sign of larger n Signed-magnitude subtractor n Change the sign of subtrahend, perform addition 36 Chapter 2: Number Systems 6
Complement Number Systems n Taking the complement is more difficult than changing the sign, but in complement system add/subt are easier. n Radix complement of D: (-D) = r n D where D is an n-digit number n If D is between 1 and r n -1 then D complement is between r n -1 and r n - (r n -1) = 1 n When D=0, D complement is r n, which is (n+1) digits, hence D complement is also 0. 37 n Two s Complement Radix complement for binary numbers D: n bit binary number ( D) = 2 n D = (2 n -1) D + 1 1710= 000100012 00002 11101110 1111 + 1 + 1 111011112 = -1710 100002 n The range of representable numbers: -(2 n-1 ) (2 n-1-1) n A number is negative iff MSB is 1. n Zero (0) is positive, one extra negative number. n Decimal equivalent of two s complement number is computed in the same way for unsigned except MSB is (- 2 n-1 ), not (2 n-1 ). n Sign extension property: extend MSB for (n+1) bit number from n bit number: 0110 = 00110 (5 bit), 1010 = 11010 (5 bit) 38 One s Complement Representation Two s complement addition n One s complement of D -D = 2 n - 1 D 17 10 = 00010001 2 11101110 2 = -17 10-3 1101-2 1110-1 1111 +1 0001 +1 0001 +1 0001-2 1110-1 1111 0 10000 n Subtract n (add n) n Add 16-n n 16-n is 4-bit two s complement of n, that is (-n). subt 1111-1 -2 1001 0000 0001 0 1 2 3-7 -8 7 1000 0111 add 39 40 Two s complement subtraction Binary Multiplication 1 c in 1 +4 0100 0100 +3 0011 0011 - +3 0011 1100 - -4 1100 0011 +1 10001 +7 0111 n Overflow n If the result exceeds the range of the number system +5 0101 + +6 0110 +11 1011 = -5 Overflow rule: Add n : if the sign of the addends are same and the sign of the sum is different from addends sign. n Unsigned binary multiplication n Shift and add multiplication 11 1011 x 13 1101 01011 00000 01011 1011 10001111 41 42 Chapter 2: Number Systems 7
n Two s complement multiplication n Shift and two s complement addition except for the last step. Remember MSB represent (-2 n-1 ) -5 1011 x 3 1101 0000 initial partial product, which is zero. 1011 11011 partial product 0000 111011 partial product 1011 11100111 0101 shifted-and-negated 1 00001111 43 Binary division n Shift and subtract with unsigned numbers n No other easy way. n So for signed numbers, take care of signs and perform shift-and-subtract 44 n BCD: Binary-Coded Decimal 0-9 encoded with their 4-bit unsigned binary representation (0000 1001). The codewords (1010 1111) are not used. n 8-bit byte represent values from 0 to 99. n BCD Addition: Carry 1 1 448 0100 0100 1000 + 489 0100 1000 1001 937 Sum 1001 1101 10001 Add 6 + 0110 + 0110 BCD sum 1 0011 1 0111 BCD result 1001 0011 0111 n GRAY CODES As we count up and down using binary codes, the number of bits that change from one to another varies. n For Gray Codes, only 1 bit changes. Decimal 8,4,2,1 Gray 0 0000 0000 1 0001 0100 2 0010 0101 3 0011 0111 4 0100 0110 5 0101 0010 6 0110 0011 7 0111 0001 8 1000 1001 9 1001 1000 45 46 Summary n Conversion between different number system n Binary Arithmetic 47 Chapter 2: Number Systems 8