Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic 1 – Number Systems. What is a Number System? A number system consists of an ordered set of symbols (digits) with relations defined for addition,

Similar presentations


Presentation on theme: "Topic 1 – Number Systems. What is a Number System? A number system consists of an ordered set of symbols (digits) with relations defined for addition,"— Presentation transcript:

1 Topic 1 – Number Systems

2 What is a Number System? A number system consists of an ordered set of symbols (digits) with relations defined for addition, subtract, multiplication, and division. The radix of a number system is the total number of digits allowed in the system. Any number in a system can have both an integer (whole) part and a fractional part, separated with a radix point.

3 Positional Notation As an example, consider a fictional paycheck of $956.81. This number is expressed in positional notation. This means that the position of each digit indicates its relative weight (or significance). Here, this paycheck can be cashed for 9 hundred dollar bills, 5 ten dollars bills, 6 one dollar bills, 8 dimes, and 1 penny.

4 Positional Notation A positive number, N, can be written in positional notation as: N = (a n-1 a n-2 …a 1 a 0 a -1..a -2 …a -m ) r Here, n represents the number of digits to the left of the radix point, m represents the number of digits to the right of the radix point, and r represents the radix. Using this notation, our paycheck can be expressed as (956.81) 10. In general, we can emit the () r if the radix is known by context.

5 Polynomial Notation We can also express this amount in polynomial notation. For example, we can express our value of 956.81 as 9 x 10 2 + 5 x 10 1 + 6 x 10 0 + 8 x 10 -1 + 1 x 10 -2 Note that each digit resides in a weighted position and that weight is a power of the radix (10 in this case).

6 Important Number Systems In the study of digital systems we have a number of important number systems… Binary - Radix 2 Octal - Radix 8 Decimal- Radix 10 Hexadecimal- Radix 16

7 Important Number Systems 0000 1111 21022 31133 410044 510155 611066 711177 81000108 91001119 10101012A 11101113B 12110014C 13110115D 14111016E 15111117F 16100002010 Decimal Binary Octal Hexadecimal

8 Binary in Digital Systems Digital systems are usually constructed in a two state device (it’s either on or off). This makes the binary number system ideally suited for representing numbers in digital systems. Only two digits, 0 and 1 (called bits) are needed. A bit can be stored in a two stage storage device known as a latch. A binary number of length n can be stored in an n-bit long device known as a register, which is built with n latches.

9 Binary Addition Binary addition is very simple. This is best shown in an example of adding two binary numbers… 1 1 1 1 0 1 + 1 0 1 1 1 --------------------- 0 1 0 1 1 1111 1100 carries

10 Binary Addition We can add four binary numbers: (101101) 2, (110101) 2, (001101) 2, and (010001) 2 … 10 10 10 10 1 10 1 0 1 1 0 1 1 1 0 1 0 1 0 0 1 1 0 1 + 0 1 0 0 0 1 ---------------------- 1 0 0 0 0 0 0 0 carries

11 Binary Subtraction We can also perform subtraction (with borrows in place of carries). Let’s subtract (10110) 2 from (1001101) 2 … 1 10 0 10 10 0 0 10 1 0 0 1 1 0 1 - 1 0 1 1 1 ------------------------ 1 1 0 1 1 0 borrows

12 Binary Multiplication Binary multiplication is much the same as decimal multiplication, except that the multiplication operations are much simpler… 1 0 1 1 1 X 1 0 1 0 ----------------------- 0 0 0 0 0 1 0 1 1 1 0 0 0 0 0 1 0 1 1 1 ----------------------- 1 1 1 0 0 1 1 0

13 Binary Division Binary division is the same trial and error procedure as decimal division… 1 1 0 1 ------------------------- 1 0 0 1 / 1 1 1 0 1 1 1 1 0 0 1 ---------- 1 0 1 1 1 0 0 1 ---------- 1 0 1 1 1 0 0 1 ---------- 1 0 remainder quotient

14 Hexadecimal Arithmetic Arithmetic in hexadecimal follows the same procedures… Adding (2A58) 16 + (71D0) 16 1 2 A 5 8 +7 1 D 0 --------------- 9 C 2 8 carries

15 Hexadecimal Arithmetic Subtracting (9F1B) 16 - (4A36) 16 E 11 9 F 1 B -4 A 3 6 --------------- 5 4 E 5 borrows

16 Hexadecimal Arithmetic Multiplying (5C2A) 16 X (71D0) 16 5 C 2 A X 7 1 D 0 ---------------------- 4 A E 2 2 0 5 C 2 A 2 8 5 2 6 ---------------------- 2 8 F 9 6 C 2 0

17 Hexadecimal Arithmetic Dividing (27FCA) 16 / (3E) 16 A 5 1 ------------------------- 3 E / 2 7 F C A 2 6 C ------- 1 3 C 1 3 6 ------- 6 A 3 E ---- 2 C quotient remainder

18 Base Conversions Often, you will need to convert numbers from one base into another. The easiest and most direct way to do this is series substitution. To convert a number from base A to base B, first form the series representation of the number in base A and the evaluate the series using base B arithmetic.

19 Series Substitutions For example, to convert (10100) 2 to base 10… N = 1(2 4 )+0(2 3 )+1(2 2 )+0(2 1 )+0(2 0 ) N = (16) 10 + 0 + (4) 10 + 0 + 0 N = (20) 10 And, to convert (1101.011) 2 to base 8… N = 1(2 3 )+1(2 2 )+0(2 1 )+1(2 0 )+0(2 -1 )+1(2 -2 )+1(2 -3 ) N = (10) 8 + (4) 8 + 0 + (1) 8 + 0 + (.2) 8 + (.1) 8 N = (5.3) 8

20 Series Substitution To convert (AF3.15) 16 to base 10… N=A(16 2 ) + F(16 1 ) + 3(16 0 ) + 1(16 -1 ) + 5(16 -2 ) N=10 10 (256 10 ) + 15 10 (16 10 ) + 3 10 (1 10 ) + 1 10 (0.0625 10 ) + 5 10 (0.00390625 10 ) N=2560 10 + 240 10 + 3 10 + 0.0625 10 + 0.01953125 10 N = (2803.08203125) 10

21 Radix Divide Method An alternate method of converting from base A to base B is the radix divide method. (1) Divide (N)A by the desired base B, producing quotient Q 1 and remainder R 0. R 0 is the least significant digit (d 0 ) of the result. (2) Compute each remaining digit by dividing the quotient Q i by (B) A, producing Q i+1 and remainder R i, which represents d i. (3) Stop when the quotient Q i+1 = 0. This is best seen with some examples…

22 Radix Divide Method Converting (234) 10 to base 8… 2 9 ------- 8 / 2 3 4 1 6 ----- 7 4 7 2 --- 2 b0b0 3 ----- 8 / 2 9 2 4 --- 5 b1b1 0 --- 8 / 3 0 - 3 b2b2 Therefore, (234) 10 = (352) 8

23 Radix Divide Method Converting (234) 10 to base 16… 1 4 ------- 16 / 2 3 4 1 6 ----- 7 4 6 4 --- 1 0 (10) 10 = (A) 16 = b 0 0 ----- 16 / 1 4 0 --- 1 4 (14) 10 = (E) 16 = b 1 Therefore, (234) 10 = (EA) 16

24 Radix Multiplication Method In order to convert fractional numbers from base A to base B, we can use the radix multiplication method. (1) Let F -1 = (N) A. (2) Compute digits (b -i ) A by multiplying F i by (B) A, producing integer I -i which represents digit (b -i ) A and fraction F -(i+1). (3) Convert each digit (b -i ) A to base B. Again, this is best seen with some examples…

25 Radix Multiplication Method Converting (0.828125) 10 to base 2… 0.828125 X 2 -------- 1.656250 b -1 0.250000 X 2 -------- 0.500000 b -5 0.500000 X 2 -------- 1.000000 b -6 0.625000 X 2 -------- 1.250000 b -4 0.656250 X 2 -------- 1.312500 b -2 0.312500 X 2 -------- 0.625000 b -3 Therefore, (0.828125) 10 = (0.110101) 2

26 Radix Multiplication Method Converting (0.1285) 10 to base 8… 0.1285 X 8 ------ 1.0280 b -1 0.0280 X 8 ------ 0.2240 b -2 0.2240 X 8 ------ 1.7920 b -3 0.7920 X 8 ------ 6.3360 b -4 0.3360 X 8 ------ 2.6880 b -5 0.6880 X 8 ------ 5.5040 b -6 0.5040 X 8 ------ 4.0320 b -7 0.0320 X 8 ------ 0.2560 b -8 Therefore, (0.1285) 10 = (0.10162540…) 8

27 General Conversion Algorithms There are two general conversion algorithms. Algorithm 1: To convert N from base A to base B, use: (a) the series substitution method with base B arithmetic, or (b) the radix divide and/or multiply methods with base A arithmetic.

28 General Conversion Algorithms Algorithm 2: To convert N from base A to base B, use: (a) the series substitution method with base 10 arithmetic to convert N from base A to base 10, and then (b) the radix divide and/or multiply methods with base 10 arithmetic to convert N from base 10 to base B.

29 General Conversion Algorithms Algorithm 1 is more direct, as we simply go from base A to base B. Algorithm 2 goes from base A to base 10 to base B. So, algorithm 2 requires more steps than algorithm 1. However, it is often easier, faster, and less error prone, as all arithmetic is performed in decimal (base 10). Bottom line … do what is easier for you.

30 Signed Number Representation The easiest and most straightforward method of representing signed numbers is the sign magnitude code. A signed number may be written as… N = (sa n-1 a n-2 …a 1 a 0 a -1 …a -m ) rsm where s = 0 if the number N is positive and s = (r-1) if the number N is negative (where r is the radix of the number system used to represent the number N).

31 Sign Magnitude Representation Therefore, the sign magnitude code of N=(-13) 10 in decimal is… N = (9, 13) 10sm And the sign magnitude code of N = (-13) 10 in binary is… N = -(13) 10 = -(1101) 2 = (1, 1101) 2sm The comma is used to delimit sign digits for the sake of clarity.

32 Sign Magnitude and Digital Systems Sign magnitude code is typically not used in digital systems. This method requires circuitry and algorithms that require the system to understand and process the sign magnitude coded numbers…a process which is both time consuming and requires complex circuitry. A much more powerful code number systems can be used in digital systems.

33 Complementary Number Systems Complementary numbers form the basis of complementary arithmetic which is a powerful method used by digital systems to handle signed number manipulation. In these systems, positive numbers are represented the same as in sign-magnitude representation; negative numbers are represented as the complement of the corresponding positive number.

34 Radix Complements The radix complement [N] r of a number (N) r is defined as: [N] r = r n – (N) r where n is the number of digits in (N) r. The largest positive number (positive full scale) that can be represented in such a scale of r n-1 -1 while the most negative number (negative full scale) is –r n-1.

35 Two’s Complement Numbers The two’s complement number system is the most commonly used system for digital systems. The two’s complement of a number (N) 2 is defined as [N] 2 = 2 n – (N) 2

36 Calculating the 2’s Complement of a Number To calculate the two’s complement of (N) 2 = (01100101) 2 … [N] 2 = [01100101] 2 = 2 8 – (01100101) 2 = (100000000) 2 – (01100101) 2 = (10011011) 2

37 2’s Complement & Negative Numbers Therefore, (10011011) 2 is the 2’s complement of (01100101) 2. We can show that this can be used to represent –(N) 2 by showing that (N) 2 +[N] 2 = 0… 1 0 0 1 1 0 1 1 +0 1 1 0 0 1 0 1 ------------------- 10 0 0 0 0 0 0 0 If we discard the carry, we can see that the 2’s complement of a binary number can be used to represent its negative counterpart.

38 Complement of the Complement We can also take the 2’s complement of the 2’s complement of a number and get the original number back… [[N] 2 ] 2 = [00101100] 2 = 2 8 – (00101100) 2 = (100000000) 2 – (00101100) 2 = (11010100) 2. Since this is the original (N) 2, we can see that [[N] 2 ] 2 = (N) 2.

39 Complement Shortcuts There also are shortcuts to computing the 2’s complement of a binary number… Algorithm 1 – Starting with the least significant bit, copy all of the bits up to and including the first 1 bit and then complementing the remaining bits. N = 0 1 1 0 0 1 0 1 [N] = 1 0 0 1 1 0 1 1

40 Complement Shortcuts N = 1 1 0 1 0 1 0 0 [N] = 0 0 1 0 1 1 0 0 For N = (10110)2 for n = 8 (each number represented in 8 bits)… N= 0 0 0 1 0 1 1 0 [N]= 1 1 1 0 1 0 1 0

41 Complement Shortcuts Algorithm 2 – Simply complement each bit and then add 1 to the result. Finding the 2’s complement of (01100101) 2 and of its 2’s complement… N= 01100101N = 10011011 10011010 01100100 + 1 + 1 --------------- 10011011 01100101

42 2’s Complement Numbers To summarize 2’s complement numbers: The two’s complement is computed using the expression [N] 2 = 2 n – (N) 2. The two’s complement of a binary number is equivalent to the inverse of that number...-(N) 2 = [N] 2. There are multiple ways of calculating [N] 2, with the simplest being to complement each bit of (N) 2 and then add one to the result.

43 Complementary Arithmetic As subtracting two numbers is equivalent to adding the complement of one number to the other number, digital systems need only adder circuitry to perform both addition and subtraction. As any digital system has a limited number of resources (and the circuits must be a fixed size), machines have fundamental limits as to the range of numbers they can represent.

44 Finite Number Representation Machines that use 2’s complement arithmetic can represent integers in the range -2 n-1 <= N <= 2 n-1 -1 where n is the number of bits available for representing N. Note that 2 n-1 -1 = (011..11) 2cns and –2 n-1 = (100..00) 2cns.

45 Adders Circuitry that performs addition (usually using 2’s complement numbers) is known as an adder. Adders have a set number of bits of precision. For instance, an adder that handles n bit numbers is referred to as a n-bit adder. Adders will produce a carry bit when their computed sum is greater than what the adder can represent in n bits. An n-bit adder will produce a carry bit for any sum A >= 2 n.

46 Overflows When an operation (such as addition) produces a result that falls outside this range, an overflow condition is said to occur. In such a case, the n-bit number produced by the operation will not be a valid representation of the result. Generally, digital systems monitor the operations they perform and generate a warning signal when overflow occurs, so invalid numbers are not mistaken for correct and valid results.

47 Complementary Arithmetic In order to see how arithmetic in the 2’s complement system works, let’s look at three cases. In each of these cases, we will assume that B >= 0 and C >= 0. A = B + C A = B – C A = – B – C

48 Case 1: A = B + C In the first case, A = B + C, we know that since B and C are nonnegative, A must also be nonnegative. The difficulty that arises here is when A>2 n-1 - 1, as this is when an overflow occurs. This is easily detected as the sign bit of A will be incorrect. To see this, let’s consider the sum of the two largest possible n-bit positive numbers…

49 Case 1: A = B + C The largest possible n-bit number is 2 n-1 -1. If we add this number to itself, (2 n-1 -1) + (2 n-1 -1) = 2 n - 2 As this is greater than 2 n-1 -1, an overflow condition occurs for any sum, A, in the range A >= 2 n-1. The nth bit of any number in this range will be set to 1. Therefore, any such result will appear to be negative, thus indicating the overflow condition.

50 Case 1 Summary To summarize A = B + C… When A > 2 n-1 -1, an overflow is indicated by the sign bit of A being incorrect. If two positive numbers are added and the result is negative, overflow has occurred. Since the maximum possible A = 2 n -2, there will never be a carry bit out of the nth bit of the binary adder.

51 Case 1 Example #1 For case 1, let’s compute (9) 10 + (5) 10 using 5-bit 2’s complement arithmetic. Since the result has a zero sign bit, it correctly represents the desired sum. Indeed, (01110) 2cns = +(1110) 2 = +(14) 10. 0 1 0 0 1 (2cns code for (9) 10 ) +0 0 1 0 1 (2cns code for (5) 10 ) -------------- 0 1 1 1 0

52 Case 1 Example #2 Now, let’s compute (12) 10 + (7) 10 using 5-bit 2’s complement arithmetic. This result has a 1 sign bit, indicating a negative number! Thus, overflow has occurred and the result is invalid. Indeed, the correct result, (19) 10 is outside the 5-bit 2’s complement range. 0 1 1 0 0 (2cns code for (12) 10 ) +0 0 1 1 1 (2cns code for (7) 10 ) -------------- 1 0 0 1 1

53 Case 2: A = B – C In this case, the computation is treated as A = B + (-C). As we’re using 2’s complement coding, we can write (-C) as [C]… A = (B) 2 + [C] 2 = (B) 2 + 2 n – (C) 2 = 2 n + (B) 2 – (C) 2

54 Case 2: A = B – C So, we can see that A = 2 n +(B) 2 –(C) 2. This seems right, except for the extra 2 n term. To understand this, let’s consider when B >= C. In this case, B-C >- 0. Therefore A >= 2 n. Since A >= 2n, this is a carry bit (recall that an adder generates a carry bit for any sum >= 2 n). This carry bit can be ignored and we arrive at the result A = B – C, which is what we want.

55 Case 2: A = B – C To be complete, let’s also consider when B < C. In this case, B-C < 0 and A = 2 n – (C – B) 2 = [C – B] 2. This gives A = -(C-B) 2, which is the correct answer. Note that here there is no carry bit generated by the adder (as there is no 2n term). When B and C are both positive numbers, B-C will always be less than either of the two numbers, meaning no overflow cannot be generated in this condition.

56 Case 2 Example #1 Let’s compute (12) 10 – (5) 10. (12) 10 = +(1100) 2 = (01100) 2cns (-5) 10 = -(0101) 2 = (11011) 2cns Adding these two 5-bit codes… Discarding the carry bit, the sign bit is seen to be zero, indicating a correct result. Indeed, (00111) 2cns = +(0111) 2 = +(7) 10. 0 1 1 0 0 +1 1 0 1 1 -------------- 10 0 1 1 1 carry

57 Case 2 Example #2 Let’s compute (5) 10 – (12) 10. (-12) 10 = -(1100) 2 = (10100) 2cns (5) 10 = +(0101) 2 = (00101) 2cns Adding these two 5-bit codes… Here, there is no carry bit and the sign bit is 1. This indicates a negative result, which is what we expect. (11001) 2cns = -(7) 10. 0 0 1 0 1 +1 0 1 0 0 -------------- 1 1 0 0 1

58 Case 3: A = –B – C Here, we will take the complements of B and C and add them together to compute A. Therefore… If the carry bit is discarded, the computation produces the correct result, the 2’s complement representation of –(B+C) 2. A = [B] 2 + [C] 2 = 2 n – (B) 2 + 2 n – (C) 2 = 2 n + 2 n – (B+C) 2 = 2 n + [B + C] 2

59 Case 3 Example #1 Let’s compute –(9) 10 – (5) 10 … -(9) 10 = -(1001) 2 = (10111) 2cns -(5) 10 = -(0101) 2 = (11011) 2cns Adding these codes… If we ignore the carry bit, we can see the sign bit is 1. Therefore, the result is correct. Indeed, (10010) 2cns = -(1110) 2 = -(14) 10. 1 0 1 1 1 +1 1 0 1 1 -------------- 11 0 0 1 0 carry

60 Negative Number Overflow Just as too large of a positive number can produce an overflow, so can too small of a negative number. Any result in the range A < -2 n-1 will cause an overflow, indicated by an incorrect sign bit (it appears to be positive). This can be illustrated…

61 Case 3 Example #2 Let’s compute –(12) 10 – (5) 10 … -(9) 10 = -(1100) 2 = (10100) 2cns -(5) 10 = -(0101) 2 = (11011) 2cns Adding these codes… If we ignore the carry bit, we can see the sign bit is 0. This indicates an overflow and the result is invalid. 1 0 1 0 0 +1 1 0 1 1 -------------- 10 1 1 1 1 carry

62 2’s Complement Summary CaseCarrySign Bit ConditionOver flow? B + C00B+C <= 2 n-1 -1no 01B+C > 2 n-1 -1yes B – C10B <= Cno 01B > Cno - B – C11-(B+C) >= -2 n-1 no 10-(B+C) < -2 n-1 yes

63 Diminished Radix Complement Number Systems In addition to the radix complement number systems (such as 2’s complement), there is a class of number systems known as the diminished radix complement systems. This is defined as… [N] r-1 = r n - (N) r -1

64 1’s Complement For binary numbers, the 1’s complement system is used. To find the 1’s complement of a n-bit binary number… [N] 2-1 = 2 n – (N) 2 – 1 As a shortcut algorithm, simply complement each bit of (N) 2.

65 1’s Complement Arithmetic Using 1’s complement numbers, subtracting a number is not the same as adding its inverse (or subtracting it). For example, suppose we wish to add +(1001) 2 and –(0100) 2. We compute the 1’s complement of (0100) 2 and obtain 11011. We add 01001 and 11011 and obtain 100100. This is not the correct result.

66 End-Around Carry The correct result, in this system, is obtained by adding the carry out of the most significant bit to the result. In this case, we take the result 00100 and add the carry out bit of 1 and obtain the result of 00101. This is the correct result. This procedure is referred to as an end- around carry and is a necessary step in diminished complement arithmetic.

67 Diminished Radix Complement Number Systems Diminished radix complement number systems (especially the 1’s complement) are not used very often. There are significant advantages to the complementary number systems. However, the 1’s complement system comes up sometimes in the study of digital systems, so it is a good idea to understand how this system of number representation works.

68 Summary You should now know: Decimal, binary, octal, and hexadecimal number systems Conversion between these bases Arithmetic operations in these bases How negative numbers can be represented in computers, which method is most commonly used, and why


Download ppt "Topic 1 – Number Systems. What is a Number System? A number system consists of an ordered set of symbols (digits) with relations defined for addition,"

Similar presentations


Ads by Google