Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2 Number Systems + Codes. Overview Objective: To use positional number systems To convert decimals to binary integers To convert binary integers.

Similar presentations


Presentation on theme: "Chapter 2 Number Systems + Codes. Overview Objective: To use positional number systems To convert decimals to binary integers To convert binary integers."— Presentation transcript:

1 Chapter 2 Number Systems + Codes

2 Overview Objective: To use positional number systems To convert decimals to binary integers To convert binary integers to decimals To represent binary integers in 3 forms: –Sign magnitude –1’s complement –2’s complement To reprensent number in octal (base 8) and hexadecimal numbers and convert them to decimal and binary numbers To understand various computer codes

3 Overview Two types of representations of information: Numeric information -Positional number system -Conversion between number systems -Operation on number systems (add, substract …) Non numeric information -Codes - Properties of codes

4 Overview Real life Data Numeric data (125.5 : a price Nonnumeric data (John : a name ) Representation of data in Computer or digital circuits 000111000000 : string of 0’s and 1’s Correspondance or Representation

5 Positional Number Systems (PNS) PNS – a number is represented by a string of digits. Each digit position is weighted by a power of the base or radix. Example: Decimal PNS, in base 10 digit position i is weighted by 10 i 937.5210=9  100 + 3  10 + 7  1 + 5 .1 + 2 .01 + 2 .001 + 0 .0001 General Form: Base or Radix = 10 D : d p-1 d p-2 d p-3 … d 2 d 1 d 0. d -1 d -2 …d -n D = d p-1.10 p-1 + d p-2.10 p-2 +…+ d 1.10 1 + d 0.10 0 + d -1.10 -1 + d -2.10 -2 + … + d -n.10 -n p -1 D =  d i.10 i i=-n I. Representation of numerical information d i is a decimal digit

6 Positional Number Systems (PNS) General Form: Base or Radix = 2 D : b p-1 b p-2 b p-3 … b 2 b 1 b 0. b -1 b -2 …b -n D = b p-1.2 p-1 + b p-2.2 p-2 +…+ b 1.2 1 + b 0.2 0 + b -1.2 -1 + b -2.2 -2 + … + b -n.2 -n p -1 D =  b i.2 i i=-n b i is a binary digit I. Representation of numerical information

7 Summary of PNS Decimal is natural (we count in base 10) - (Radix or Base 10 ) digits  {0,1,…,8,9} Binary is used in digital system - (Radix or Base 2 ) digits  {0,1} Octal is used for representing multibits (group of 3 bits) numbers in digital systems. - (Radix or Base 8=2 3 ) digits  {0,1,…,6,7} Hexadecimal is used for representing multibits (group of 4 bits) numbers in digital systems - (Radix or Base 16=2 4 ) digits  {0,1,…9,A,B,…,F} I. Representation of numerical information

8 Summary of PNS (…) Dec Binary Oct Hex 000 (000)0 (0000) 1 011 (001)1 (0001) 2102 (010)2 (0010) 3113 (011)3 (0011) 41004 (100)4 (0100) 51015 ( 101)5 (0101) 61106 (110)6 (0110) 71117 (111)7 (0111) 8100010 (001 000)8 (1000) 9100111 (001 001)9 (1001) 10101012 (001 010)A (1010) 11101113 (001 011)B (1011) 12110014 (001 100)C (1100) 13110115 (001 101)D (1101) 14111016 (001 110)E (1110) 15111117 (001 111)F (1111) 161000020 (010 000)10 (0001 0000) I. Representation of numerical information

9 Conversions From any base r ---> Decimal Use base 10 arythmetic to expand : p -1 D =  d i.r i (r is the base and di are the digits) i=0 Two solutions can be used to do this : Polynomial expansion D = d p-1.r p-1 + d p-2.r p-2 +…+ d 1.r 1 + d 0.r 0 Examples : 310 16 = 3.16 2 + 1.16 1 + 0.16 0 = 3.256 + 16 + 0 = 784 10 1CE8 16 = 1.16 3 + 12.16 2 + 14. 16 1 + 8.16 0 = 7400 10 436.5 8 = 4.8 2 + 3. 8 1 + 6.8 0 + 5. 8 -1 = 286.625 10 I. Representation of numerical information

10 Conversions From any base r ---> Decimal Iterative multiplication (Expansion of D) D can be written as : ((….((d p-1).r + d p-2).r +…+d 2 ).r + d 1 ).r + d 0 Examples : 310 16 = ((3).16+ 1).16 + 0) = (49).16 + 0 = 784 10 Good for programming I. Representation of numerical information

11 Conversions From Decimal ---> Any base r Successive divisions of D by r yield the digits from the least to the most significant bit. Remember that D can be written in base r as : D = d p-1.r p-1 + d p-2.r p-2 +…+ d 1.r + d 0. = ((….((d p-1).r + d p-2).r +…+d 2 ).r + d 1 ).r + d 0 D/r -----> quotient D1 = (….((dp-1).r + dp-2).r +…+d 2 ).r + d 1 remainder d0 D1/r -----> quotient D2 = (….((dp-1).r + dp-2).r +… d3).r+d 2 ) remainder d1 D2/r -----> quotient D3 = (….((dp-1).r + dp-2).r +…+d 3 ) remainder d2 And so on ….. I. Representation of numerical information

12 Conversions From Decimal ---> Any base r Example : conversion of 179 from Base 10 -> Base 2 179 10 179 1 89 1 LSB 2 44 1 4 22 0 8 11 0 16 5 1 32 2 1 64 1 0 128 0 1 MSB weight Successive divisions by 2 179 10 = 10110011 2 I. Representation of numerical information

13 Conversions From Decimal ---> Any base r Example : conversion of 467 from Base 10 -> Base 8 467 3 1 58 3 LSB 16 8 7 2 448 64 0 7 MSB 467 weight successive divisions 467 10 = 723 8 I. Representation of numerical information

14 Other Conversions From Binary ---> Octal/Hex Conversion by substitution –Binary to Oct : Starting from rightmost bit, make groups of 3 bits and convert each group to base8 100011001110 2 = 100/011/001/110 = 4316 8 4 / 3 / 1 / 6 –Binary to Hex : Starting from rightmost bit, make groups of 4 bits and convert each group to base 16 100011001110 2 = 1000/1100/1110 = 8CE 16 8 / C / E I. Representation of numerical information

15 Other Conversions From Octal/hex ---> Binary Conversion by substitution – Octal to Binary : Convert each digit to a 3 bits binary number 576 8 = 101 111 110 2 5 7 6 –Hex to Binary : Convert each digit to a 4 bits binary number 576 16 = 0101 0111 0110 2 5 7 6 I. Representation of numerical information

16 Addition of binary numbers Example 1 (Addition) Carry 1 01111000 X 190 10111110 + Y 141 10001101 331 1 01001011 Result has more bits than binary addends Carry equals 1 in the last bit position Example 2 (Addition) Carry 0 01011000 X 173 10101101 + Y 44 00101100 217 11011001 Carry equal to 0 in the last bit position I. Representation of numerical information

17 Addition of binary numbers In general, given two binary number X = ( x n-1 … x i … x 1 x 0 ) 2 and Y = ( y n-1 … y i … y 1 y 0 ) 2 the sum (X+Y) bit by bit at position i is given by (c i is the ith position’s carry) c i-1 Addition table + x i ci-1 xi yi si ci + y i 0 0 0 0 0 c i s i 0 0 1 1 0 0 1 0 1 0 0 1 1 0 1 1 0 0 1 0 1 0 1 0 1 1 1 0 0 1 1 1 1 1 1 I. Representation of numerical information

18 Substraction of binary numbers Substraction B 0 10 1 1 0 1 1 0 1 0 10 X 1 2 1 29 1 1 1 0 0 1 0 1 - Y 46 0 0 1 0 1 1 1 0 183 1 0 1 1 0 1 1 1 B 0 1 0 1 0 1 1 0 10 0 10 X 2 1 10 1 1 0 1 0 0 1 0 - Y 1 09 0 1 1 0 1 1 0 1 1 01 1 1 0 0 1 0 1 I. Representation of numerical information

19 Addition of hexadecimal numbers C 1 1 X 1 9 B 9 16 + Y C 7 E 6 16 E 1 9 F 16 I. Representation of numerical information Addition of two hexadecimal numbers

20 Representation of negative numbers Signed-magnitude systems +99, -57, -13.3 Binary uses a (sign bit, the most significant bit (MSB)) MSB = 0 (Positive) MSB = 1 (Negative) 0 1010101 2 = + 85 10 1 1010101 2 = -85 10 0 1111111 2 = + 127 10 1 1111111 2 = -127 10 0 0000000 2 = + 0 10 1 0000000 2 = - 0 10 Problem: Too much logic needed to - detect and compare sign bit - add or abstract magnitudes - determine the sign of the result I. Representation of numerical information

21 Representation of negative numbers Complement number system Difficult to change to complement But adding or substracting two numbers are easier once they are represented in complement number systems I. Representation of numerical information

22 Representation of negative numbers Radix – Complement representation n digit number is substracted from r n Example: if r = 10 and n = 4, r n = 10 4 =10,000 The 10’s complement of D=1849 10 is: (r n – D) = 8151 Computing 10’s complement: r n – D = (r n - 1 – D) +1 (r n - 1) – D => complement of each digit with rspect to base (r-1) In base 10 complement each digit with respect to 9 and 1 to the result Example: The 10’s complement of 1849 10 is (10 4 - 1849)= (9999 – 1849) + 1 = 8 1 5 0 + 1 = 8151 I. Representation of numerical information

23 Representation of negative numbers Radix – Complement representation Complement of each digit : r –1 – d i In base 10 : 10 –1 – d i = 9 – d i Example : The complement of each digit of 345 is : For 5 : (10 – 1) – 5 = 9 – 5 = 4 For 4 : = 9 – 4 = 5 For 3 : = 9 – 3 = 6 In base 3 the radix complement of 121 is 101 In base 2 the radix complement of 1110111 is 0001000 I. Representation of numerical information

24 Representation of negative numbers Two’s Complement The MSB is the sign bit Example: 17 10 = 00010001 2 -127 10 = 10000001 2 11101110 01111110 +1 +1 11101111 2 = -17 10 01111111 =127 10 The range is -(2 n-1 ) -> (2 n-1 -1), For n=8 0 10 = 00000000 2 -128 10 = 10000000 2 I. Representation of numerical information Note: The weight of the MSB is –2 n-1 The range of positive numbers is 0 to 127 The range of negative numbers is –1 to -128

25 Representation of negative numbers Two’s Complement Two’s complement of 0 10 and -128 10 0 10 = 00000000 2 -128 10 = 10000000 2 11111111 01111111 +1 +1 100000000 2 = 0 10 10000000 = -128 10 I. Representation of numerical information Note: Ignore carry out of the MSB position Note: -128 does not have a positive counterpart, that is –128 is its own Complement : this can creat problems ……

26 2’s Complement Addition + Substraction +3 0011-2 0010 -> 1101+1-> 1110 +4 +0100 + -6 0110 -> 1001+1-> + 1010 +7 0111 -8 11000 +6 0110 0110 + -3 0011 -> 1100+1 => 1101 +3 1 0011 = 3 I. Representation of numerical information Overflow in the last position Rule : Ignore any carry beyond the MSB. The result is correct if the range of the number system Is not exceeded.

27 2’s Complement Addition + Substraction I. Representation of numerical information Addition/Substraction rule : Assume that the signed integers are represented in 2s complement : To compute A – B, compute the 2’s complement B’ of B and add B’ to A Examples : 50101 50101 +20010 - 2 + 1110 ( = 2’) 7 0111 3 1 0011 (ignore the leftmost carry

28 Overflow Addition of 2 numbers with different signs can never overflow Addition of 2 numbers with same sign -3 1101 +5 0101 + -6 1010 + +6 0110 -9 1 0111 = +7 +11 1011 = -5wrong If signs of (addends) are same and sign of sum is different I. Representation of numerical information

29 Substraction rules (2’s complement) +4 0100 0100 - +3 -0011 1101 1 1 0001 - 3 1101 1101 - -4 1100 0011 1 1 0001 Same overflow rules apply I. Representation of numerical information

30 Binary multiplication 11 1011  13 1101 33 1011 11 1 0000 143 1 1011 1 1011 10001111 uses a shift register and adder, and logic control Signed multiplication +  + = + +  - = - -  - = + I. Representation of numerical information

31 Binary codes for decimal numbers Goal: Use binary strings (sequence of 0’s and 1’s) to represent decimal information Definition: A code is a set of n-bit strings. Each n-bit string represrnts a different number. A code word is a particular member of a code II. Representation of non numerical information

32 Binary codes for decimal numbers A code is a set of n-bit strings. Each n-bit string represrnts a different number. A code word is a particular member of a code If n bit is used to represent each code words, the total number of possible code words is 2 n Examples: To encode a set consisting of 3 elements {A,B,C} we need 2 bits to represent each code word. The possible code words are {00, 01, 10, 11} II. Representation of non numerical information

33 Binary codes for decimal numbers We need to assign a code word to each element of the original set. Assignment 1 : 00 -----> A 01 -----> B 10 -----> C Code 11 is unused Another assignment : 10 -----> A 01 -----> B 11 -----> C Code 00 is unused II. Representation of non numerical information

34 Binary codes for decimal numbers To encode the 10 decimal digits {0,1,….,8,9} We need 4 bit binary code words. There are 16=2 4 possible code words. So 6 codes are unused. Assignment 1 : 0 -----> 00005 -----> 0101 1-----> 00016 -----> 0110 2-----> 00107 -----> 0111 3-----> 00118 -----> 1000 4-----> 01009 -----> 1001 The following codes are unused: 1010, 1011, 1100, 1101, 1110, 1111 II. Representation of non numerical information


Download ppt "Chapter 2 Number Systems + Codes. Overview Objective: To use positional number systems To convert decimals to binary integers To convert binary integers."

Similar presentations


Ads by Google