220 likes | 246 Views
This lecture covers positional number systems including binary, octal, and hexadecimal, as well as conversions between them. It also explains addition and subtraction of binary numbers, representation of negative numbers using sign/magnitude and complements, with examples for better comprehension.
E N D
EECE 320Digital Systems DesignLecture 2: Number Systems and Codes Ali Chehab
Number Systems and Codes • A digital designer must establish a correspondence between binary digits and real-life numbers, events and conditions
Positional Number Systems • A number is represented by a string of digits • Each digit has an associated weight • Number = weighted sum of digits • EX1: 534 = 5x100 + 3x10 + 4x1 • The weights are power of 10: 100, 10, 1 • EX2: 85.28 = 8x10 + 5x1 + 2x0.1 + 8x0.01 • We have positive and negative powers • This system had a base (radix, r) of 10 • General form of a number is: • dp-1dp-2 … d1d0.d-1d-2 … d-n • The vule of the number is: • dp-1 = Most significant digit and d-n = least significant digit
Positional Number Systems • Binary: radix = 2 • Used to represent numbers in a digital system • Reliable since only 2 values need to be distinguished • EX: 110.01 = 1x4 + 1x2 + 0x1 + 0x0.5 + 1x0.25 = 6.2510 • In general the value is given by: • Octal and Hexadecimal represent a shorthand notation for the binary system • Octal (Base 8): { 0, 1, 2, 3, 4, 5, 6, 7 } • Example: 576 = 6*80 + 7*81 + 5*82 = 38210 • Hexadecimal (Base 16): { 0, …, 9, A, B, C, D, E, F } • Example: A2F = F*160 + 2*161 + A*162 • = 15*160 + 2*161 + 10*162 = 260710
General Positional Number System Conversions • To convert a number D to decimal, we convert each digit to radix-10 equivalent and expand the weighted-sum formula using radix-10 arithmetic • EX1: 1CE8.116 = 1x163 + 12x162 + 14x16 + 8x1 + 1x16-1 • = 7400.062510 • EX2: F1AC16 = ((15x16 + 1)x16 + 10)x16 + 12 = 6186810
Result: 4610 = 1011102 General Positional Number System Conversions • Decimal to Binary: It is done by successive divisions until a zero quotient is obtained. The remainders taken in reverse order constitute the binary representation Example: Convert 46 Decimal into Binary 46 / 2 = 23 and Remainder = 0 23 / 2 = 11 and Remainder = 1 11 / 2 = 5 and Remainder = 1 5 / 2 = 2 and Remainder = 1 2 / 2 = 1 and Remainder = 0 1 / 2 = 0 and Remainder = 1
General Positional Number System Conversions • Decimal fraction to binary: It is done by successive multiplications taking out each time the integer part of the result. • Example: convert 0.3125 to binary • 0.3125 x 2 = 0.625 0 • 0.625 x 2 = 1.25 1 • 0.25 x 2 = 0.5 0 • 0.5 x 2 = 1 1 • 0.312510 = 01012
General Positional Number System Conversions • Binary to Octal: Bits are grouped in “Threes” from right to left, and the binary value of every group represents the corresponding Octal Digit • Example: 1 0 0 1 1 1 0 0 1 0 1 • 2 3 4 5 • Result: 100111001012 = 23458 • Binary to Hexadecimal: Similar, but in groups of four. • Example: 1 1 0 1 1 1 0 0 1 0 1 1 1 0 • 3 7 2 E • Result: 110111001011102 = 372E16
Addition and Subtraction of Binary Numbers 190 10111110 +141 10001101 331 101001011 229 11100101 - 46 00101110 183 10110111
Representation of Negative Numbers • Negative integers can be represented using sign/magnitude, one’s complement or two’s complement notations • Sign and Magnitude: Leftmost bit is used to indicate the sign • 0 positive and 1 negative • Example using 4 bits: +5 = 01 0 1 and –5 = 11 0 1 • Range: from -7 to +7 (from 1111 to 0111) (-(2N-1-1) to 2N-1-1) • Disadvantages • two representations of 0 (0000, 1000) • The process of addition needs a relatively complex adder
Complements • Complements are used in digital systems to negate a number and to simplify the subtraction operation. • Two types of complements for any base-r system: • Radix complement: called r’s complement • Diminished radix complement: called (r-1)’s complement • In base ten: 10’s complement, 9’s complement • In base 2: 2’s complement, 1’s complement • Diminished radix: Given a number N in base r having n digits, the (r-1)’s complement is defined as follows:
Complements • EX: r=10, what is 10n? • It is a single ‘1’ followed by n 0s. EX: 104 = 1,0000(ten) • EX: r=10, what is 10n - 1? • It is n 9s. 104 -1 = 9999 • EX: What is 9’s complement of 546700? • EX: r=2, what is 24? 10000 • EX: r=2, what is 24-1? 1111 • 1’s complement: subtract each digit from 1. But 1-0=1, 1-1=0 => to get 1’s complement of a number, flip all its digits.
Complements • Radix complement: Given a number N in base r having n digits, the r’s complement is defined as follows: • Since • r’s complement is obtained by taking (r-1)’s complement and adding 1. • EX: 2’s complement of 101100 is • 010011 + 1 = 010100 • EX: 10’s complement of 012398 is • 987601 + 1 = 987602
Complements • Short cuts: • 10’s complement: leave all least significant 0s unchanged, subtract first nonzero least significant digit from 10, and subtract all higher significant digits from 9. • 2’s complement: leave all least significant 0s unchanged, leave the first 1 unchanged, flip all other higher significant digits. • What is the r’s complement of the r’s complement of a number?
Representation of Negative Numbers • Two’s Complement: A negative # is represented by complementing each bit of the corresponding positive # and adding 1. • MSB has a weight of -2N-1 instead of 2N-1 • A negative number has MSB = 1 • Example using 8 bits: +9 = 0 0 0 0 1 0 0 1 • -9 = 1 1 1 1 0 1 1 1 • Range is from –128 to +127 (10000000 to 01111111) • For N bits, the range is: -(2N-1) to +(2N-1-1) • Overflow and underflow occur when result is outside the range • 1 representation of 0. Using 4 bits: 0000 • Addition can be easily carried out: A carry will be ignored. • Example: to add –1 and –2 using 4 bits: 1 1 1 1 1 1 1 0 ---------- Ignored 1 1 1 0 1 = -3
Representation of Negative Numbers +7 +6 +5 +4 +3 +2 +1 +0 -0 -1 -2 -3 -4 -5 -6 -7 -8
2’s Complement Addition: Overflow • An overflow is said to occur if the result of the addition exceeds the range. • Addition of 2 numbers with different signs cannot produce an overflow • Examples • -3 1101 +5 0101 • + -6 1010 + +6 0110 • ---- -------- ----- ------- • -9 1 0111 = +7 +11 1011 = -5 • A simple rule to detect overflow is to check if the sign of the result is different from the sign of the addends.
Subtraction Rules • Most subtraction circuits for 2’s complement numbers do not perform subtraction. Instead, we take the two’s complement of the subtrahend and add the 2 numbers: A – B = A + (-B) • We can complement bit by bit the subtrahend, added to the minuend and add a carry of 1 • Examples • 1 Cin1 Cin • +4 0100 0100 +3 0011 0011 • - +3 - 0011 + 1100 - +4 - 0100 + 1011 • ---- -------- ------ ----- ------- ------ • +1 1 0001 -1 1111 • We can use the same rules of addition to detect overflow • We can subtract -8 as long as the result does not overflow • (-3) – (-8) = (1101) – (1000) = (1101) + (0111) + 1 = 1 0101 = +5
Binary Codes for decimal Numbers • Some digital devices process decimal numbers • The interface of a digital system may read or display decimal #s • A decimal number is represented by a string of at least 4 bits
Binary Codes for decimal Numbers • Gray Code • Only 1 bit changes between each pair of consecutive codewords • It is a reflective code • For a 1 bit code 0 and 1 • For (N+1)-bit code, the first 2N are the same of N-bit code with a leading zero • The last 2N codewords are the reverse of N-bit code with a leading 1
Next Lecture and Reminders • Next lecture • Reminders